是否可以为SELECT中的每一行执行存储过程?这只执行第一行,查找要为所有行执行的操作:
Declare
@Loop bit = 1, @ID int, @Exists bit, @ReturnValue bit = 0
WHILE (@Loop) = 1
BEGIN
SELECT @ID = ID FROM Table --Multiple Rows Returned
EXEC [dbo].[StoredProc1] --Exec SP for Each Row
@InputID = @ID
,@Exists = @Exists OUTPUT
IF @Exists = 1
BEGIN
SET @Loop = 0
SET @ReturnValue = 1
END
END
SELECT @ReturnValue [ReturnValue]
答案 0 :(得分:5)
使用光标:
DECLARE @exists bit
DECLARE db_cursor CURSOR FOR
SELECT ID
FROM Table
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @ID
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC [dbo].[StoredProc1] --Exec SP for Each Row
@ID
, @exists OUTPUT
FETCH NEXT FROM db_cursor INTO @id
END
CLOSE db_cursor
DEALLOCATE db_cursor