SQL脚本 — 行变列

create table aaa
(
col nvarchar(10)
)

insert into aaa
select 1
union
select 2
union
select 3

declare @sql varchar(8000)
set @sql=’select ‘
select  @sql=@sql+’max(case col when ”’+ col +”’ then col end) + ”,” + ‘
from (select distinct col from aaa)  a
set @sql=left(@sql,len(@sql)-1)

set @sql=@sql+’  from aaa’
–print @sql
exec(@sql)

drop table aaa

发表评论

电子邮件地址不会被公开。

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>