SQL中的逗号分隔列表
问题描述:
如何通过SQL中的逗号分隔列表循环?我有一个ID列表,我需要将这些ID传递给存储过程。我无法改变存储过程。我需要弄清楚如何为每个ID执行SP。给我一些想法,我可以从那里继续。SQL中的逗号分隔列表
谢谢。
答
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
你想呼吁每个ID seperately存储过程?你在SQL以外使用哪种语言? – n8wrl 2012-04-05 15:40:13
如果你不能改变存储,为什么不多次调用它? – Soader03 2012-04-05 15:40:45
什么版本的SQL Server? – Yuck 2012-04-05 15:41:03