使用表数据作为参数运行存储过程 [英] Run Stored Procedure with table data as parameter

查看:32
本文介绍了使用表数据作为参数运行存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 id_table 中有多个 id,我需要对 table1 中的至少多少行运行此过程.我正在使用 while 循环来运行循环,直到表 1 中的计数完成,但谁能告诉我如何每次更改 @ID.

I have number of ids in id_table and I need to run this procedure for at least how many rows in table1. I am using while loop to run loop till counts finish in table1 but can anyone tell me how can I change @ID every time.

如果有人能告诉我如何在 c# 中做也可以.

If anyone can tell me how to do in c# will also be fine.

declare @ID INT
declare @noRun1 INT
declare @howTime INT

set @noRun1=1
set @howTime = (select count(*) from table1)
set @ID =(select top 1 id from id_table)

while (@noRun1<=@howTime)
begin 
    EXEC proc_run @ID
set @noRun1=@noRun1+1
end

推荐答案

试试这个

    DECLARE @uniqueId int
DECLARE @TEMP TABLE (uniqueId int)
-- Insert into the temporary table a list of the records to be updated
INSERT INTO @TEMP (uniqueId) SELECT uniqueId FROM myTable

-- Start looping through the records
WHILE EXISTS (SELECT * FROM @TEMP)
BEGIN
-- Grab the first record out
SELECT Top 1 @uniqueId = uniqueId FROM @TEMP
PRINT 'Working on @uniqueId = ' + CAST(@uniqueId as varchar(100))
-- Perform some update on the record
EXEC proc_run @uniqueId
-- Drop the record so we can move onto the next one
DELETE FROM @TEMP WHERE uniqueId = @uniqueId
END

这篇关于使用表数据作为参数运行存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆