declare @IDList as varchar(max) declare @ID as int declare @i as int set @IDList='' select @IDList=@IDList + cast(id as varchar(50)) + ',' from table while CharIndex(',',@IDList)<>0 Begin set @ID = SubString(@IDList,1,CharIndex(',',@IDList)-1) print(@ID) set @IDList = Replace(','+@IDList,','+Cast(@ID as nvarchar(50))+',','') End