SQL SERVER 循环处理一组数据

可以通过分割字符串的方式来实现
代码如下:

DECLARE @PointerPrev int
DECLARE @PointerCurr int
DECLARE @data varchar(30)
DECLARE @datalist varchar(400)
Set @PointerPrev=1
Set	@datalist='194,202,200,201,203,206,204,205,207,211,208,210,212,57,58,59,60,47,53,56,52,51'
while (@PointerPrev < LEN(@datalist ))
	Begin
    	Set @PointerCurr=CharIndex(',',@datalist ,@PointerPrev)
        if(@PointerCurr>0)
	        Begin
	        	set @data =SUBSTRING(@datalist ,@PointerPrev,@PointerCurr-@PointerPrev) 
				print(@data)
	            SET @PointerPrev = @PointerCurr+1
	        End
        else
            Break
    End

结果如下:
SQL SERVER 循环处理一组数据_第1张图片

你可能感兴趣的:(日常)