T-SQL: Looping through an array of known values

后端 未结 7 1574
深忆病人
深忆病人 2021-01-30 00:51

Here\'s my scenario:

Let\'s say I have a stored procedure in which I need to call another stored procedure on a set of specific ids; is there a way to do this?

i

相关标签:
7条回答
  • 2021-01-30 01:26

    I usually use the following approach

    DECLARE @calls TABLE (
        id INT IDENTITY(1,1)
        ,parameter INT
        )
    
    INSERT INTO @calls
    select parameter from some_table where some_condition -- here you populate your parameters
    
    declare @i int
    declare @n int
    declare @myId int
    select @i = min(id), @n = max(id) from @calls
    while @i <= @n
    begin
        select 
            @myId = parameter
        from 
            @calls
        where id = @i
    
            EXECUTE p_MyInnerProcedure @myId
        set @i = @i+1
    end
    
    0 讨论(0)
提交回复
热议问题