我已经声明了一个临时表来保存所有必需的值,如下所示:
DECLARE @temp TABLE
(
Password int,
IdTran int,
Kind varchar(16)
)
INSERT INTO @temp
SELECT s.Password, s.IdTran, 'test'
from signal s inner join vefify v
on s.Password = v.Password
and s.IdTran = v.IdTran
and v.type = 'DEV'
where s.[Type] = 'start'
AND NOT EXISTS (SELECT * FROM signal s2
WHERE s.Password = s2.Password
and s.IdTran = s2.IdTran
and s2.[Type] = 'progress' )
INSERT INTO @temp
SELECT s.Password, s.IdTran, 'test'
from signal s inner join vefify v
on s.Password = v.Password
and s.IdTran = v.IdTran
and v.type = 'PROD'
where s.[Type] = 'progress'
AND NOT EXISTS (SELECT * FROM signal s2
WHERE s.Password = s2.Password
and s.IdTran = s2.IdTran
and s2.[Type] = 'finish' )
现在我需要遍历@temp表中的行,并且对于每一行调用一个sp,它将@temp表的所有参数作为输入。 我怎样才能做到这一点?
答案 0 :(得分:71)
你可以使用光标:
DECLARE @id int
DECLARE @pass varchar(100)
DECLARE cur CURSOR FOR SELECT Id, Password FROM @temp
OPEN cur
FETCH NEXT FROM cur INTO @id, @pass
WHILE @@FETCH_STATUS = 0 BEGIN
EXEC mysp @id, @pass ... -- call your sp here
FETCH NEXT FROM cur INTO @id, @pass
END
CLOSE cur
DEALLOCATE cur
答案 1 :(得分:1)
尝试将数据集从存储过程返回到C#或VB.Net中的数据表。然后,可以使用批量复制将数据表中的大量数据复制到目标表中。我已经使用BulkCopy将包含数千行的大型数据表加载到Sql表中,并在性能方面取得了巨大成功。
您可能希望在C#或VB.Net代码中试用BulkCopy。
答案 2 :(得分:1)
DECLARE maxval, val, @ind INT;
SELECT MAX(ID) as maxval FROM table;
while (ind <= maxval ) DO
select `value` as val from `table` where `ID`=ind;
CALL fn(val);
SET ind = ind+1;
end while;
答案 3 :(得分:0)
您可以这样做
Declare @min int=0, @max int =0 --Initialize variable here which will be use in loop
Declare @Recordid int,@TO nvarchar(30),@Subject nvarchar(250),@Body nvarchar(max) --Initialize variable here which are useful for your
select ROW_NUMBER() OVER(ORDER BY [Recordid] ) AS Rownumber, Recordid, [To], [Subject], [Body], [Flag]
into #temp_Mail_Mstr FROM Mail_Mstr where Flag='1' --select your condition with row number & get into a temp table
set @min = (select MIN(RecordID) from #temp_Mail_Mstr); --Get minimum row number from temp table
set @max = (select Max(RecordID) from #temp_Mail_Mstr); --Get maximum row number from temp table
while(@min <= @max)
BEGIN
select @Recordid=Recordid, @To=[To], @Subject=[Subject], @Body=Body from #temp_Mail_Mstr where Rownumber=@min
-- You can use your variables (like @Recordid,@To,@Subject,@Body) here
-- Do your work here
set @min=@min+1 --Increment of current row number
END
答案 4 :(得分:-2)
您始终不需要光标。你可以用while循环来完成它。你应该尽可能避免使用游标。虽然循环比游标快。