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

posted @ 2011-09-23 07:28  】Richard【  阅读(174)  评论(0编辑  收藏  举报