如何通过存储过程运行一系列值
我有一个存储过程,需要运行值列表并将其输出到临时表中。
这是 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 的编写方式和预期目的。这两个值与州和县的关系相关,由前两列 ParentHeaderId
和 HeaderId
表示。我选择使用 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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样的事情:
Something like this: