练习行列转换 sql server 2000
依据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]
--动态方法
--三个单引号,其中有两个单引号是转义字符,两个单引号相当于一个单引号,还有一个单引号是连接字符串用的
相关文档:
---------数学函数 ---------------
1.绝对值
S:select abs(-1) value
O:select abs(-1) value from dual
2.取整(大)
S:select ceiling(-1.001) value
O:select ceil(-1.001) value from dual
3.取整(小)
S:select floor(-1.001) value
O:select floor(-1.001) value from ......
declare @tmp table
(
id int identity(1,1),
TableName varchar(100),
Column_name varchar(100),
Type varchar(50),
Lenght int,
Scale int,
Nullable varchar(1),
Defaults varchar(4000),
PrimaryKey varchar(1)
)
select iid = identity(int,1,1), * into #a from SysObjects where xtype = 'U'
declare ......
数据类型比较
类型名称
Oracle
SQLServer
比较
字符数据类型 CHAR CHAR 都是固定长度字符资料但oracle 里面最大度为2kb,SQLServer里面最大长度为8kb
变长字符数据类型 VARCHAR2 VARCHAR Oracle 里面最大长度为 4kb,SQLServer里面最大长度为8kb
根据字符集而定的固定长度字符串 NCHAR NCHAR 前者最大长度2kb ......
SQL Native Client ODBC Driver
标准安全连接
Driver={SQL Native Client};Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;
您是否在使用SQL Server 2005 Express 请在“Server”选项使用连接表达式“主机名称\SQLEXPRESS”。
受信的连接
Driver={SQL Native ......
IN
确定给定的值是否与子查询或列表中的值相匹配。
EXISTS
指定一个子查询,检测行的存在。
比较使用 EXISTS 和 IN 的查询
这个例子比较了两个语义类似的查询。第一个查询使用 EXISTS 而第二个查询使用 IN。注意两个查询返回相同的信息。
USE pubs
GO
SELECT DISTINCT pub_name
from publishers
WHERE ......