--方法一:
DECLARE @id VARCHAR(200)
DECLARE @idlist VARCHAR(500) = '1,2,3,5,6,8,9,10,11,12,14,15,16,17,18,19,20'
WHILE LEN(@idlist) > 0
BEGIN
IF CHARINDEX(',',@idlist) > 0
SET @id = SUBSTRING(@idlist,0,CHARINDEX(',',@idlist))
ELSE
BEGIN
SET @id = @idlist
SET @idlist = ''
END
PRINT (@id)
--SET @idlist = REPLACE(@idlist,@id + ',' , '') --用replace会出现意外
set @IDList = stuff(@IDList,1,charindex(',',@IDList),'')
END
--方法二:
DECLARE @IDList VARCHAR(200)
DECLARE @sTempData VARCHAR(50)
DECLARE @Iindex INT
SET @IDList = '1,2,3,5,6,8,9,10,11,12,14,15,16,17,18,19,20'
WHILE CHARINDEX(',', @IDList) > 0
BEGIN
SET @Iindex = CHARINDEX(',', @IDList)
SET @sTempData = SUBSTRING(@IDList, 0, @Iindex )
--SET @IDList = SUBSTRING(@IDList, @Iindex + 1, LEN(@IDList) - @Iindex)
set @IDList = stuff(@IDList,1,charindex(',',@IDList),'')
PRINT(@sTempData)
END
PRINT(@IDList)
效果截图: