Comma separated list in SQL

前端 未结 1 1892
[愿得一人]
[愿得一人] 2020-12-24 06:18

How to loop through comma separated list in SQL? I have a list of ID\'s and I need to pass these ID\'s to a stored procedure. I CANNOT alter the stored procedure. I need to

相关标签:
1条回答
  • 2020-12-24 07:10
    declare @S varchar(20)
    set @S = '1,2,3,4,5'
    
    while len(@S) > 0
    begin
      --print left(@S, charindex(',', @S+',')-1)
      exec YourSP left(@S, charindex(',', @S+',')-1)
      set @S = stuff(@S, 1, charindex(',', @S+','), '')
    end
    

    Try on SE Data: Walk the string

    0 讨论(0)
提交回复
热议问题