首页 文章

SQL中的逗号分隔列表

提问于
浏览
27

如何在SQL中循环逗号分隔列表?我有一个ID列表,我需要将这些ID传递给存储过程 . 我不能改变存储过程 . 我需要弄清楚如何为每个id执行SP . 给我一些想法,我可以从那里继续 .

谢谢 .

1 回答

  • 71
    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
    

    试试SE数据:Walk the string

相关问题