----------------------------------------------------------------
-- Author  :DBA_Huangzj(發糞塗牆)
-- Date    :2013-11-26 08:49:28
-- Version:
--      Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64) 
-- Dec 28 2012 20:23:12 
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go 
create table [huang]([NAME] varchar(1))
insert [huang]
select 'A' union all
select 'B' union all
select 'C'
--------------开始查询--------------------------declare @s nvarchar(4000)
set @s=''
Select     @s=@s+','+quotename('NAME'+CONVERT(VARCHAR(2),ROW_NUMBER()OVER(ORDER BY GETDATE())))+'=max(case when [NAME]='+quotename([NAME],'''')+' then [NAME] else null end)'
from [huang] --group by[Course]
SET @s=SUBSTRING(@s,2,LEN(@s))
exec('select '+@s+' from [huang] ')
 
 
----------------结果----------------------------
/* 
NAME1 NAME2 NAME3
----- ----- -----
A     B     C
*/