T-SQL: Looping through an array of known values

后端 未结 7 1585
深忆病人
深忆病人 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:05

    You can try as below :

    declare @list varchar(MAX), @i int
    select @i=0, @list ='4,7,12,22,19,'
    
    while( @i < LEN(@list))
    begin
        declare @item varchar(MAX)
        SELECT  @item = SUBSTRING(@list,  @i,CHARINDEX(',',@list,@i)-@i)
        select @item
    
         --do your stuff here with @item 
         exec p_MyInnerProcedure @item 
    
        set @i = CHARINDEX(',',@list,@i)+1
        if(@i = 0) set @i = LEN(@list) 
    end
    

提交回复
热议问题