这是我所知道的最好方法。如果有人知道更好的解决方案,请发帖:
sal
DECLARE @id INT , @max INT , @dep INT , @all VARCHAr(255)SELECt @id = 1 , @max = MAX(id)FROM salSELECt * INTO #tmp FROM salWHILE (1=1)BEGIN SELECt @dep = dept FROM #tmp WHERe id = @id IF @dep IS NULL BEGIN SELECT @id = @id + 1 IF @id > @max BREAK ELSE ConTINUE END UPDATE #tmp SET @all = @all + ',' + ConVERT(VARCHAR, id) WHERe dept = @dep --remove last comma select @all = RIGHt(@all, LEN(@all)-1) DELETE #tmp WHERe dept = @dep -- selecting the output. insert into table if you want SELECT @dep, @all SELECT @dep = NULL , @all = NULL SELECT @id = @id + 1 IF @id > @max BREAK -- fail safe IF @id > 100 BREAKENDdrop table #tmp
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)