我运行查询select @id=table.id from table
,我需要遍历结果,以便可以对每一行执行存储过程exec stored_proc @varName=@id,@otherVarName='test'
如何在T-SQL脚本中执行此操作?
我运行查询select @id=table.id from table
,我需要遍历结果,以便可以对每一行执行存储过程exec stored_proc @varName=@id,@otherVarName='test'
如何在T-SQL脚本中执行此操作?
Answers:
在这种情况下,您可以使用CURSOR:
DECLARE @id INT
DECLARE @name NVARCHAR(100)
DECLARE @getid CURSOR
SET @getid = CURSOR FOR
SELECT table.id,
table.name
FROM table
OPEN @getid
FETCH NEXT
FROM @getid INTO @id, @name
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC stored_proc @varName=@id, @otherVarName='test', @varForName=@name
FETCH NEXT
FROM @getid INTO @id, @name
END
CLOSE @getid
DEALLOCATE @getid
修改以显示表中的多个参数。
@name
我认为可以使用的新参数,但是自从我现在使用光标以来已经有一段时间了!
您可以执行以下操作:
create procedure test
as
BEGIN
create table #ids
(
rn int,
id int
)
insert into #ids (rn, id)
select distinct row_number() over(order by id) as rn, id
from table
declare @id int
declare @totalrows int = (select count(*) from #ids)
declare @currentrow int = 0
while @currentrow < @totalrows
begin
set @id = (select id from #ids where rn = @currentrow)
exec stored_proc @varName=@id, @otherVarName='test'
set @currentrow = @currentrow +1
end
END
我更喜欢的解决方案是Microsoft KB 111401 http://support.microsoft.com/kb/111401。
该链接涉及3个示例:
本文介绍了可用于模拟存储过程,触发器或Transact-SQL批处理中的类似游标的FETCH-NEXT逻辑的各种方法。
/*********** example 1 ***********/
declare @au_id char( 11 )
set rowcount 0
select * into #mytemp from authors
set rowcount 1
select @au_id = au_id from #mytemp
while @@rowcount <> 0
begin
set rowcount 0
select * from #mytemp where au_id = @au_id
delete #mytemp where au_id = @au_id
set rowcount 1
select @au_id = au_id from #mytemp
end
set rowcount 0
/********** example 2 **********/
declare @au_id char( 11 )
select @au_id = min( au_id ) from authors
while @au_id is not null
begin
select * from authors where au_id = @au_id
select @au_id = min( au_id ) from authors where au_id > @au_id
end
/********** example 3 **********/
set rowcount 0
select NULL mykey, * into #mytemp from authors
set rowcount 1
update #mytemp set mykey = 1
while @@rowcount > 0
begin
set rowcount 0
select * from #mytemp where mykey = 1
delete #mytemp where mykey = 1
set rowcount 1
update #mytemp set mykey = 1
end
set rowcount 0
DECLARE @id INT
DECLARE @name NVARCHAR(100)
DECLARE @getid CURSOR
SET @getid = CURSOR FOR
SELECT table.id,
table.name
FROM table
WHILE 1=1
BEGIN
FETCH NEXT
FROM @getid INTO @id, @name
IF @@FETCH_STATUS < 0 BREAK
EXEC stored_proc @varName=@id, @otherVarName='test', @varForName=@name
END
CLOSE @getid
DEALLOCATE @getid
试试这个:
declare @i tinyint = 0,
@count tinyint,
@id int,
@name varchar(max)
select @count = count(*) from table
while (@i < @count)
begin
select @id = id, @name = name from table
order by nr asc offset @i rows fetch next 1 rows only
exec stored_proc @varName = @id, @otherVarName = 'test', @varForName = @name
set @i = @i + 1
end
C
样式循环,但是我怀疑这样做是否可行。也许是一个PHP
样式循环,但是我再次怀疑这是否也会奏效。我需要在T-SQL中完成所有操作,因为我可以在SMS中运行SQL,而无需使用外部语言进行循环...首先要解决这个问题。