日期:2014-05-17 浏览次数:20589 次
create table celebrity ([id] int identity(1,1) primary key, [type] int , [name] nvarchar(10), [createDate] datetime, [Salary] decimal(18,2), [Field_1] decimal(18,2), [Field_2] nvarchar(100) ) insert into celebrity([type],[name],[createDate],[Salary],[Field_1],[Field_2]) select 0,'杜甫','2010-02-05',100,null,'贫困潦倒' union all select 1,'杜甫',null,200,119,'大诗人' union all select 0,'元芳','2012-02-06',5000,300,'大人,此事被后一定有一个天大的秘密' union all select 1,'元芳','2012-02-07',5100,300,'大人真乃神人也' select row_number() over(order by name, case [File] when 'createDate' then 1 when 'Salary' then 2 when 'Field_1' then 3 when 'Field_2' then 4 end,id) 'id',type,name,[File],value from (select id,type,name,col 'File',v 'value' from (select id,type,name, isnull(convert(nvarchar(50),createDate,23),'Null') 'createDate', isnull(cast(Salary as nvarchar(50)),'Null') 'Salary', isnull(cast(Field_1 as nvarchar(50)),'Null') 'Field_1', isnull(cast(Field_2 as nvarchar(50)),'Null') 'Field_2' from celebrity) c unpivot(v for col in(createDate,Salary,Field_1,Field_2)) u ) t /* id type name File value -------------------- ----------- ---------- ---------------- ----------------------------------- 1 0 杜甫 createDate 2010-02-05 2 1 杜甫 createDate Null 3 0 杜甫 Salary 100.00 4 1 杜甫 Salary 200.00 5 0 杜甫 Field_1 Null 6 1 杜甫 Field_1 119.00 7 0 杜甫 Field_2 贫困潦倒 8 1 杜甫 Field_2 大诗人 9 0 元芳 createDate 2012-02-06 10 1 元芳 createDate 2012-02-07 11