如何通过存储过程运行一系列值

发布于 2025-01-19 20:50:45 字数 1314 浏览 1 评论 0原文

我有一个存储过程,需要运行值列表并将其输出到临时表中。

这是 SP:EXEC [SP_ReturnHTML] @zoneid, 1

我假设第一个值将是一个变量,第二个值将是硬编码的。我无法修改此 SP,因为它已在其他进程中使用,因此我需要通过游标或 WHILE 循环通过 SP 运行这些值。这些值只需要运行一次,因此基于对游标的一些初步阅读(我的经验极其有限),FAST_FORWARD 游标类型可能更理想。这就是我尝试过的:

declare @zoneid int = (select zoneid from #values)
declare list cursor fast_forward
for EXEC [SP_ReturnHTML] @zoneid,1
open list
fetch next from list

但是当我尝试这样做时,我收到错误关键字“EXEC”附近的语法不正确。

使用@zoneid=14105<时此SP的输出/code> (硬编码的 1 与 fieldgroupid 相关)看起来像下面的镜头。为了清楚起见,尽管使用了 @zoneid=14105,但出现 4054 值的原因是 SP 的编写方式和预期目的。这两个值与州和县的关系相关,由前两列 ParentHeaderIdHeaderId 表示。我选择使用 14105 作为示例,因为 #values 表中的 3 个示例仅检索其辅助值,我想避免此处混淆。

输入图片这里的描述

我需要通过 SP 为 @zoneid 运行的值位于一个表中(大约有 3100 行),可以通过以下示例进行说明

create table #values (zoneid int)  
insert into #values 
values
(13346),
(13347),
(13348)

:非常简单把,我需要类似下面的东西作为最终产品(伪代码):

declare @zoneid INT = (select zoneid from #values)
select * into #results from 
(
EXEC [SP_ReturnHTML] @zoneid, 1
)

I have a stored procedure that I need to run a list of values through and output into a temp table.

This is the SP: EXEC [SP_ReturnHTML] @zoneid, 1

The first value, I assume, will be a variable and the second value will be hard-coded. I am not able to modify this SP, as it is used in other processes, so I need to run these values through the SP via a cursor or WHILE loop. The values only need to be run through once, so a FAST_FORWARD cursor type may be more ideal, based on some preliminary reading on cursors (of which my experience in is extremely limited). This is what I attempted:

declare @zoneid int = (select zoneid from #values)
declare list cursor fast_forward
for EXEC [SP_ReturnHTML] @zoneid,1
open list
fetch next from list

But when I try to do this, I get the error Incorrect syntax near the keyword 'EXEC'.

The output of this SP, when using @zoneid=14105 (and the hard-coded 1 relates to the fieldgroupid) looks something like the shot below. For clarity, despite using @zoneid=14105, the reason a value of 4054 shows up is due to the way the SP is written, and is intended. The two values relate to a state and county relationship, noted by the first 2 columns, ParentHeaderId and HeaderId. I opted to use 14105 for the example, because the 3 examples in the #values table only retrieve their secondary value and I wanted to avoid confusion here.

enter image description here

The values that I need to run through the SP for the @zoneid are in a table (which has about 3100 rows), which can be exemplified with the following:

create table #values (zoneid int)  
insert into #values 
values
(13346),
(13347),
(13348)

So very simply put, I need something like the following as a final product (pseudo code):

declare @zoneid INT = (select zoneid from #values)
select * into #results from 
(
EXEC [SP_ReturnHTML] @zoneid, 1
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余生共白头 2025-01-26 20:50:45

像这样的事情:

drop table if exists #results 
drop table if exists #Data
go

create or alter procedure [SP_ReturnHTML] @value int, @s varchar(20)
as
begin
  select concat(' value=',@value, '; s = ', @s) 
end

go

create table #Data (value int, county varchar(30))
insert into #Data 
values
(100, 'Baker'),
(101,'Baldwin'),
(102,'Baldwin'),
(103,'Ballard'),
(104,'Baltimore City'),
(105,'Baltimore'),
(106,'Bamberg'),
(107,'Bandera'),
(108,'Banders'),
(109,'Banks'),
(110,'Banner'),
(111,'Bannock'),
(112,'Baraga')

go

create table #results(value nvarchar(200))
declare c cursor local for select value from #Data
declare @value int
open c
fetch next from c into @value
while @@fetch_status = 0
begin
  insert into #results(value)
  EXEC [SP_ReturnHTML] @value, '1'

  fetch next from c into @value
end

go

select *  
from #results 

Something like this:

drop table if exists #results 
drop table if exists #Data
go

create or alter procedure [SP_ReturnHTML] @value int, @s varchar(20)
as
begin
  select concat(' value=',@value, '; s = ', @s) 
end

go

create table #Data (value int, county varchar(30))
insert into #Data 
values
(100, 'Baker'),
(101,'Baldwin'),
(102,'Baldwin'),
(103,'Ballard'),
(104,'Baltimore City'),
(105,'Baltimore'),
(106,'Bamberg'),
(107,'Bandera'),
(108,'Banders'),
(109,'Banks'),
(110,'Banner'),
(111,'Bannock'),
(112,'Baraga')

go

create table #results(value nvarchar(200))
declare c cursor local for select value from #Data
declare @value int
open c
fetch next from c into @value
while @@fetch_status = 0
begin
  insert into #results(value)
  EXEC [SP_ReturnHTML] @value, '1'

  fetch next from c into @value
end

go

select *  
from #results 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文