【SQL开源代码栏目提醒】:网学会员为需要SQL开源代码的朋友们搜集整理了223.sql相关资料,希望对各位网友有所帮助!
--用Sql Server 2005数据库
use db_Sql
--如果数据表存在,将该数据表删除
if exists(select * from INFORMATION_SCHEMA.TABLES
where table_name = 'tb_unpivot10')
drop table tb_unpivot10
GO
--创建数据表
create table tb_unpivot10
(编号 int, 数字1 int, 数字2 int,
数字3 int, 数字4 int, 数字5 int)
GO
--向表中添加字段
INSERT INTO tb_unpivot10 VALUES (1,6,4,9,6,6)
INSERT INTO tb_unpivot10 VALUES (8,6,1,9,9,9)
INSERT INTO tb_unpivot10 VALUES (4,6,4,9,6,6)
INSERT INTO tb_unpivot10 VALUES (6,6,8,9,9,6)
INSERT INTO tb_unpivot10 VALUES (9,9,1,9,9,9)
GO
--Unpivot the table.
SELECT 编号, 数字, Orders
FROM
(SELECT 编号, 数字1, 数字2, 数字3, 数字4, 数字5
FROM tb_unpivot10) p
UNPIVOT
(Orders FOR 数字 IN
(数字1, 数字2, 数字3, 数字4, 数字5)
)AS unpvt
GO