现在的位置: 首页 > 综合 > 正文

sql游标实现行列转换

2012年08月21日 ⁄ 综合 ⁄ 共 582字 ⁄ 字号 评论关闭

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

抱歉!评论已关闭.