执行一系列元素的过程(执行theProc(从表中选择id))

Omu*_*Omu 3 sql sql-server

我需要为deleteQuestion这个select查询返回的每个元素执行过程:

select id from questions where Stuff = @Stuff

execute deleteQuestion id
Run Code Online (Sandbox Code Playgroud)

就像是:
execute deleteQuestion each(select id fom questions where Stuff = @Stuff)

谁知道怎么样?

Kla*_*sen 5

使用游标:

DECLARE @Id INT
DECLARE your_cursor CURSOR FOR 
SELECT id from questions where Stuff = @Stuff 

OPEN your_cursor 

FETCH NEXT FROM your_cursor 
INTO @Id

WHILE @@FETCH_STATUS = 0
BEGIN

    execute deleteQuestion @Id

    FETCH NEXT FROM your_cursor 
        INTO @Id

END 
CLOSE your_cursor
DEALLOCATE your_cursor
Run Code Online (Sandbox Code Playgroud)