sql游标实现行列转换


if exists (select object_id(N'tempdb..#T'))
begin
drop table  #T
end
create Table #T
(
s nvarchar(4000),
id int
)
declare @a nvarchar(4000)
declare @idc int
DECLARE   @tablename   nvarchar(256)
DECLARE   tnames_cursor   CURSOR
FOR  

 SELECT   sortID   FROM   Alink group  by sortID

OPEN   tnames_cursor
FETCH   NEXT   FROM   tnames_cursor   INTO   @tablename
WHILE   (@@FETCH_STATUS   =   0)
BEGIN
   
set @idc=@tablename
select  @a=isnull(name,' ')+','+ isnull(@a,' ') from Alink where sortID=@idc  AND PASSED=0
insert into #T (s,id)values(@a,@idc)
FETCH NEXT FROM tnames_cursor into @tablename
END
CLOSE   tnames_cursor
DEALLOCATE   tnames_cursor
select * from #T

你可能感兴趣的:(行列转换)