依据csdn高手写的自己练习一下方便以后查找
--Creator:Gongl
--Date:2009-1-8
--sql server 2000
--学习行转列,为了进一步了解动态sql拼接(单双三引号)
--几种类型
--Numeric(10,2) 指字段是数字型,长度为10 小数为两位
--varchar和nvarchar的区别
--1.从存储方式上,nvarchar是按字符存储的,而 varchar是按字节存储的;
--2.从存储量上考虑, varchar比较节省空间,因为存储大小为字节的实际长度,而 nvarchar是双字节存储;
--3.在使用上,如果存储内容都是英文字符而没有汉字等其他语言符号,建议使用varchar;含有汉字的使用nvarchar,因为nvarchar是使用Unicode编码,即统一的字符编码标准,会减少乱码的出现几率;
----行转列
--创建测试数据
if object_id('idl') is not null drop table idl
create table idl(name varchar(10),subject nvarchar(10),score numeric(4,1))
insert into idl
select 'anny','数学',95.5 union all
select 'anny','语文',90 union all
select 'anny','英语',99 union all
select 'anny','asp.net',100 union all
select 'anny','sqlserver',100 union all
select 'jenny','数学',94.5 union all
select 'jenny','语文',59.5 union all
select 'jenny','asp.net',100
--静态方法1
select [name],
max(case subject when '数学' then score else 0 end) [数学],
max(case subject when '语文' then score else 0 end) [语文],
max(case subject when '英语' then score else 0 end) [英语],
max(case subject when 'asp.net' then score else 0 end) [asp.net],
max(case subject when 'sqlserver' then score else 0 end) [sqlserver]
from idl
group by [name]
--静态方法2
select [name],
max(isnull((case subject when '数学' then score end),0)) [数学],
max(isnull((case subject when '语文' then score end),0)) [语文],
max(isnull((case subject when '英语' then score end),0)) [英语],
isnull(max(case subject when 'asp.net' then score end),0) [asp.net],
isnull(max(case subject when 'sqlserver' then score end),0) [sqlserver]
from idl
group by [name]
--动态方法
--三个单引号,其中有两个单引号是转义字符,两个单引号相当于一个单引号,还有一个单引号是连接字符串用的
由于最近要用到打包SQL Server 2005 Express Edition ,由于在安装过程中需要设置很多东西。带来很多的不便,于是在网上和MSDN找了好久终于找到办法了 ^_^
我用的是批处理来实现的,好了直接贴代码
CLS
@ECHO OFF
ECHO.
ECH ......