把长日期转换为短日期 Convert(char(10),getdate(),120)
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
MS-SQL数据库开发常用汇总 1.按姓氏笔画排序:
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
Select * From TableName Order By CustomerName Collate Chinese_PRC_Stroke_ci_as
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
2.数据库加密:
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
select encrypt('原始密码')
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
select pwdencrypt('原始密码')
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
select pwdcompare('原始密码','加密后密码') = 1--相同;否则不相同 encrypt('原始密码')
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
select pwdencrypt('原始密码')
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
select pwdcompare('原始密码','加密后密码') = 1--相同;否则不相同
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
3.取回表中字段:
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
declare @list varchar(1000),@sql nvarchar(1000)
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
select @list=@list+','+b.name from sysobjects a,syscolumns b where a.id=b.id and a.name='表A'
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
set @sql='select '+right(@list,len(@list)-1)+' from 表A'
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
exec (@sql)
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
4.查看硬盘分区:
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
EXEC master..xp_fixeddrives
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
5.比较A,B表是否相等:
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
if (select checksum_agg(binary_checksum(*)) from A)
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
=
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
(select checksum_agg(binary_checksum(*)) from B)
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
print '相等'
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
else
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
print '不相等'
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
6.杀掉所有的事件探察器进程:
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
DECLARE hcforeach CURSOR GLOBAL FOR SELECT 'kill '+RTRIM(spid) FROM master.dbo.sysprocesses
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
WHERE program_name IN('SQL profiler',N'SQL 事件探查器')
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
EXEC sp_msforeach_worker '?'
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
7.记录搜索:
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
开头到N条记录
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
Select Top N * From 表
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
-------------------------------
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
N到M条记录(要有主索引ID)
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
Select Top M-N * From 表 Where ID in (Select Top M ID From 表) Order by ID Desc
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
----------------------------------
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
N到结尾记录
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
Select Top N * From 表 Order by ID Desc
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
8.如何修改数据库的名称:
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
sp_renamedb 'old_name', 'new_name'
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
9:获取当前数据库中的所有用户表
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
select Name from sysobjects where xtype='u' and status>=0
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
10:获取某一个表的所有字段
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
select name from syscolumns where id=object_id('表名')
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
11:查看与某一个表相关的视图、存储过程、函数
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
select a.* from sysobjects a, syscomments b where a.id = b.id and b.text like '%表名%'
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
12:查看当前数据库中所有存储过程
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
select name as 存储过程名称 from sysobjects where xtype='P'
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
13:查询用户创建的所有数据库
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
select * from master..sysdatabases D where sid not in(select sid from master..syslogins where name='sa')
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
或者
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
select dbid, name AS DB_NAME from master..sysdatabases where sid <> 0x01
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
14:查询某一个表的字段和数据类型
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
select column_name,data_type from information_schema.columns
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
where table_name = '表名'
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
[n].[标题]:
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
Select * From TableName Order By CustomerName
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
[n].[标题]:
来自
http://dev.csdn.net/develop/article/83/83138.shtm
一、 只复制一个表结构,不复制数据
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
select top 0 * into [t1] from [t2]
二、 获取数据库中某个对象的创建脚本
1、 先用下面的脚本创建一个函数
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
if exists(select 1 from sysobjects where id=object_id('fgetscript') and objectproperty(id,'IsInlineFunction')=0)
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
drop function fgetscript
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
go
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
create function fgetscript(
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
@servername varchar(50) --服务器名
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
,@userid varchar(50)='sa' --用户名,如果为nt验证方式,则为空
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
,@password varchar(50)='' --密码
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
,@databasename varchar(50) --数据库名称
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
,@objectname varchar(250) --对象名
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
) returns varchar(8000)
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
as
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
begin
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
declare @re varchar(8000) --返回脚本
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
declare @srvid int,@dbsid int --定义服务器、数据库集id
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
declare @dbid int,@tbid int --数据库、表id
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
declare @err int,@src varchar(255), @desc varchar(255) --错误处理变量
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
--创建sqldmo对象
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
exec @err=sp_oacreate 'sqldmo.sqlserver',@srvid output
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
if @err<>0 goto lberr
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
--连接服务器
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
if isnull(@userid,'')='' --如果是 Nt验证方式
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
begin
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
exec @err=sp_oasetproperty @srvid,'loginsecure',1
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
if @err<>0 goto lberr
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
exec @err=sp_oamethod @srvid,'connect',null,@servername
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
end
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
else
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
exec @err=sp_oamethod @srvid,'connect',null,@servername,@userid,@password
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
if @err<>0 goto lberr
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
--获取数据库集
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
exec @err=sp_oagetproperty @srvid,'databases',@dbsid output
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
if @err<>0 goto lberr
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
--获取要取得脚本的数据库id
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
exec @err=sp_oamethod @dbsid,'item',@dbid output,@databasename
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
if @err<>0 goto lberr
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
--获取要取得脚本的对象id
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
exec @err=sp_oamethod @dbid,'getobjectbyname',@tbid output,@objectname
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
if @err<>0 goto lberr
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
--取得脚本
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
exec @err=sp_oamethod @tbid,'script',@re output
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
if @err<>0 goto lberr
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
--print @re
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
return(@re)
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
lberr:
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
exec sp_oageterrorinfo NULL, @src out, @desc out
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
declare @errb varbinary(4)
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
set @errb=cast(@err as varbinary(4))
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
exec master..xp_varbintohexstr @errb,@re out
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
set @re='错误号: '+@re
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
+char(13)+'错误源: '+@src
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
+char(13)+'错误描述: '+@desc
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
return(@re)
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
end
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
go
2、 用法如下
用法如下,
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
print dbo.fgetscript('服务器名','用户名','密码','数据库名','表名或其它对象名')
3、 如果要获取库里所有对象的脚本,如如下方式
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
declare @name varchar(250)
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
declare #aa cursor for
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
select name from sysobjects where xtype not in('S','PK','D','X','L')
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
open #aa
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
fetch next from #aa into @name
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
while @@fetch_status=0
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
begin
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
print dbo.fgetscript('onlytiancai','sa','sa','database',@name)
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
fetch next from #aa into @name
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
end
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
close #aa
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
deallocate #aa
4、 声明,此函数是csdn邹建邹老大提供的
三、 分隔字符串
如果有一个用逗号分割开的字符串,比如说"a,b,c,d,1,2,3,4",如何用t-sql获取这个字符串有几个元素,获取第几个元素的值是多少呢?因为t-sql里没有split函数,也没有数组的概念,所以只能自己写几个函数了。
1、 获取元素个数的函数
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
create function getstrarrlength (@str varchar(8000))
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
returns int
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
as
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
begin
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
declare @int_return int
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
declare @start int
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
declare @next int
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
declare @location int
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
select @str =','+ @str +','
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
select @str=replace(@str,',,',',')
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
select @start =1
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
select @next =1
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
select @location = charindex(',',@str,@start)
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
while (@location <>0)
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
begin
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
select @start = @location +1
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
select @location = charindex(',',@str,@start)
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
select @next =@next +1
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
end
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
select @int_return = @next-2
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
return @int_return
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
end
2、 获取指定索引的值的函数
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
create function getstrofindex (@str varchar(8000),@index int =0)
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
returns varchar(8000)
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
as
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
begin
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
declare @str_return varchar(8000)
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
declare @start int
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
declare @next int
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
declare @location int
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
select @start =1
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
select @next =1 --如果习惯从0开始则select @next =0
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
select @location = charindex(',',@str,@start)
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
while (@location <>0 and @index > @next )
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
begin
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
select @start = @location +1
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
select @location = charindex(',',@str,@start)
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
select @next =@next +1
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
end
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
if @location =0 select @location =len(@str)+1 --如果是因为没有逗号退出,则认为逗号在字符串后
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
select @str_return = substring(@str,@start,@location -@start) --@start肯定是逗号之后的位置或者就是初始值1
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
if (@index <> @next ) select @str_return = '' --如果二者不相等,则是因为逗号太少,或者@index小于@next的初始值1。
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
return @str_return
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
end
3、 测试
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
SELECT [dbo].[getstrarrlength]('1,2,3,4,a,b,c,d')
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
SELECT [dbo].[getstrofindex]('1,2,3,4,a,b,c,d',5)
四、 一条语句执行跨越若干个数据库
我要在一条语句里操作不同的服务器上的不同的数据库里的不同的表,怎么办呢?
第一种方法:
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
select * from OPENDATASOURCE('SQLOLEDB','Data Source=远程ip;User ID=sa;Password=密码').库名.dbo.表名
第二种方法:
先使用联结服务器:
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
EXEC sp_addlinkedserver '别名','','MSDASQL',NULL,NULL,'DRIVER={SQL Server};SERVER=远程名;UID=用户;PWD=密码;'
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
exec sp_addlinkedsrvlogin @rmtsrvname='别名',@useself='false',@locallogin='sa',@rmtuser='sa',@rmtpassword='密码'
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
GO
然后你就可以如下:
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
select * from 别名.库名.dbo.表名
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
insert 库名.dbo.表名 select * from 别名.库名.dbo.表名
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
select * into 库名.dbo.新表名 from 别名.库名.dbo.表名
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
go
五、 怎样获取一个表中所有的字段信息
蛙蛙推荐:怎样获取一个表中所有字段的信息
先创建一个视图
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
Create view fielddesc
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
as
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
select o.name as table_name,c.name as field_name,t.name as type,c.length as
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
length,c.isnullable as isnullable,convert(varchar(30),p.value) as desp
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
from syscolumns c
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
join systypes t on c.xtype = t.xusertype
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
join sysobjects o on o.id=c.id
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
left join sysproperties p on p.smallid=c.colid and p.id=o.id
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
where o.xtype='U'
查询时:
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
Select * from fielddesc where table_name = '你的表名'
还有个更强的语句,是邹建写的,也写出来吧
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
SELECT
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
(case when a.colorder=1 then d.name else '' end) N'表名',
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
a.colorder N'字段序号',
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
a.name N'字段名',
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
(case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end) N'标识',
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
(case when (SELECT count(*)
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
FROM sysobjects
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
WHERE (name in
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
(SELECT name
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
FROM sysindexes
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
WHERE (id = a.id) AND (indid in
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
(SELECT indid
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
FROM sysindexkeys
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
WHERE (id = a.id) AND (colid in
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
(SELECT colid
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
FROM syscolumns
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
WHERE (id = a.id) AND (name = a.name))))))) AND
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
(xtype = 'PK'))>0 then '√' else '' end) N'主键',
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
b.name N'类型',
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
a.length N'占用字节数',
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
COLUMNPROPERTY(a.id,a.name,'PRECISION') as N'长度',
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as N'小数位数',
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
(case when a.isnullable=1 then '√'else '' end) N'允许空',
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
isnull(e.text,'') N'默认值',
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
isnull(g.[value],'') AS N'字段说明'
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
--into ##tx
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
FROM syscolumns a left join systypes b
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
on a.xtype=b.xusertype
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
inner join sysobjects d
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
left join syscomments e
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
on a.cdefault=e.id
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
left join sysproperties g
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
on a.id=g.id AND a.colid = g.smallid
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
order by object_name(a.id),a.colorder
六、 时间格式转换问题
因为新开发的软件需要用一些旧软件生成的一些数据,在时间格式上不统一,只能手工转换,研究了一下午写了三条语句,以前没怎么用过convert函数和case语句,还有"+"操作符在不同上下文环境也会起到不同的作用,把我搞晕了要,不过现在看来是差不多弄好了。
1、把所有"70.07.06"这样的值变成"1970-07-06"
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
UPDATE lvshi
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
SET shengri = '19' + REPLACE(shengri, '.', '-')
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
WHERE (zhiyezheng = '139770070153')
2、在"1970-07-06"里提取"70","07","06"
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
SELECT SUBSTRING(shengri, 3, 2) AS year, SUBSTRING(shengri, 6, 2) AS month,
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
SUBSTRING(shengri, 9, 2) AS day
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
FROM lvshi
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
WHERE (zhiyezheng = '139770070153')
3、把一个时间类型字段转换成"1970-07-06"
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
UPDATE lvshi
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
SET shenling = CONVERT(varchar(4), YEAR(shenling))
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
+ '-' + CASE WHEN LEN(MONTH(shenling)) = 1 THEN '0' + CONVERT(varchar(2),
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
month(shenling)) ELSE CONVERT(varchar(2), month(shenling))
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
END + '-' + CASE WHEN LEN(day(shenling)) = 1 THEN '0' + CONVERT(char(2),
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
day(shenling)) ELSE CONVERT(varchar(2), day(shenling)) END
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
WHERE (zhiyezheng = '139770070153')
七、 分区视图
分区视图是提高查询性能的一个很好的办法
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
--看下面的示例
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
--示例表
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
create table tempdb.dbo.t_10(
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
id int primary key check(id between 1 and 10),name varchar(10))
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
create table pubs.dbo.t_20(
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
id int primary key check(id between 11 and 20),name varchar(10))
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
create table northwind.dbo.t_30(
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
id int primary key check(id between 21 and 30),name varchar(10))
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
go
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
--分区视图
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
create view v_t
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
as
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
select * from tempdb.dbo.t_10
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
union all
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
select * from pubs.dbo.t_20
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
union all
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
select * from northwind.dbo.t_30
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
go
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
--插入数据
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
insert v_t select 1 ,'aa'
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
union all select 2 ,'bb'
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
union all select 11,'cc'
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
union all select 12,'dd'
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
union all select 21,'ee'
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
union all select 22,'ff'
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
--更新数据
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
update v_t set name=name+'_更新' where right(id,1)=1
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
--删除测试
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
delete from v_t where right(id,1)=2
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
--显示结果
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
select * from v_t
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
go
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
--删除测试
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
drop table northwind.dbo.t_30,pubs.dbo.t_20,tempdb.dbo.t_10
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
drop view v_t
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
/**//*--测试结果
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
id name
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
----------- ----------
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
1 aa_更新
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11 cc_更新
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21 ee_更新
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
(所影响的行数为 3 行)
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
==*/
八、 树型的实现
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
--参考
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
--树形数据查询示例
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
--作者: 邹建
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
--示例数据
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
create table [tb]([id] int identity(1,1),[pid] int,name varchar(20))
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
insert [tb] select 0,'中国'
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
union all select 0,'美国'
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
union all select 0,'加拿大'
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
union all select 1,'北京'
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
union all select 1,'上海'
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
union all select 1,'江苏'
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
union all select 6,'苏州'
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
union all select 7,'常熟'
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
union all select 6,'南京'
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
union all select 6,'无锡'
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
union all select 2,'纽约'
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
union all select 2,'旧金山'
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
go
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
--查询指定id的所有子
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
create function f_cid(
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
@id int
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
)returns @re table([id] int,[level] int)
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
as
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
begin
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
declare @l int
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
set @l=0
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
insert @re select @id,@l
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
while @@rowcount>0
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
begin
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
set @l=@l+1
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
insert @re select a.[id],@l
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
from [tb] a,@re b
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
where a.[pid]=b.[id] and b.[level]=@l-1
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
end
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
/**//**//**//*--如果只显示最明细的子(下面没有子),则加上这个删除
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
delete a from @re a
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
where exists(
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
select 1 from [tb] where [pid]=a.[id])
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
--*/
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
return
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
end
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
go
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
--调用(查询所有的子)
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
select a.*,层次=b.[level] from [tb] a,f_cid(2)b where a.[id]=b.[id]
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
go
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
--删除测试
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
drop table [tb]
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
drop function f_cid
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
go
九、 排序问题
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
CREATE TABLE [t] (
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
[id] [int] IDENTITY (1, 1) NOT NULL ,
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
[GUID] [uniqueidentifier] NULL
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
) ON [PRIMARY]
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
GO
下面这句执行5次
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
insert t values (newid())
查看执行结果
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
select * from t
1、 第一种
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
select * from t
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
order by case id when 4 then 1
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
when 5 then 2
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
when 1 then 3
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
when 2 then 4
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
when 3 then 5 end
2、 第二种
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
select * from t order by (id+2)%6
3、 第三种
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
select * from t order by charindex(cast(id as varchar),'45123')
4、 第四种
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
select * from t
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
WHERE id between 0 and 5
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
order by charindex(cast(id as varchar),'45123')
5、 第五种
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
select * from t order by case when id >3 then id-5 else id end
6、 第六种
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
select * from t order by id / 4 desc,id asc
十、 一条语句删除一批记录
首先id列是int标识类类型,然后删除ID值为5,6,8,9,10,11的列,这里的cast函数不能用convert函数代替,而且转换的类型必须是varchar,而不能是char,否则就会执行出你不希望的结果,这里的"5,6,8,9,10,11"可以是你在页面上获取的一个chkboxlist构建成的值,然后用下面的一句就全部删
除了,比循环用多条语句高效吧应该。
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
delete from [fujian] where charindex(','+cast([id] as varchar)+',',','+'5,6,8,9,10,11,'+',')>0
还有一种就是
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
delete from table1 where id in(1,2,3,4
![](http://ghd258.cnblogs.com/Images/dot.gif)
)
十一、获取子表内的一列数据的组合字符串
下面这个函数获取05年已经注册了的某个所的律师,唯一一个参数就是事务所的名称,然后返回zhuce字段里包含05字样的所有律师。
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
CREATE FUNCTION fn_Get05LvshiNameBySuo (@p_suo Nvarchar(50))
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
RETURNS Nvarchar(2000)
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
AS
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
BEGIN
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
DECLARE @LvshiNames varchar(2000), @name varchar(50)
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
select @LvshiNames=''
![](http://onlytiancai.cnblogs.com/Images/OutliningIndicators/None.gif)
DECLARE lvshi_cursor CURSOR FOR
数据库里有1,2,3,4,5 共5条记录,要用一条sql语句让其排序,使它排列成4,5,1,2,3,怎么写?
--
数据操作
SELECT
--
从数据库表中检索数据行和列
INSERT
--
向数据库表添加新数据行
DELETE
--
从数据库表中删除数据行
UPDATE
--
更新数据库表中的数据
--
数据定义
CREATE
TABLE
--
创建一个数据库表
DROP
TABLE
--
从数据库中删除表
ALTER
TABLE
--
修改数据库表结构
CREATE
VIEW
--
创建一个视图
DROP
VIEW
--
从数据库中删除视图
CREATE
INDEX
--
为数据库表创建一个索引
DROP
INDEX
--
从数据库中删除索引
CREATE
PROCEDURE
--
创建一个存储过程
DROP
PROCEDURE
--
从数据库中删除存储过程
CREATE
TRIGGER
--
创建一个触发器
DROP
TRIGGER
--
从数据库中删除触发器
CREATE
SCHEMA
--
向数据库添加一个新模式
DROP
SCHEMA
--
从数据库中删除一个模式
CREATE
DOMAIN
--
创建一个数据值域
ALTER
DOMAIN
--
改变域定义
DROP
DOMAIN
--
从数据库中删除一个域
--
数据控制
GRANT
--
授予用户访问权限
DENY
--
拒绝用户访问
REVOKE
--
解除用户访问权限
--
事务控制
COMMIT
--
结束当前事务
ROLLBACK
--
中止当前事务
SET
TRANSACTION
--
定义当前事务数据访问特征
--
程序化SQL
DECLARE
--
为查询设定游标
EXPLAN
--
为查询描述数据访问计划
OPEN
--
检索查询结果打开一个游标
FETCH
--
检索一行查询结果
CLOSE
--
关闭游标
PREPARE
--
为动态执行准备SQL 语句
EXECUTE
--
动态地执行SQL 语句
DESCRIBE
--
描述准备好的查询
--
-局部变量
declare
@id
char
(
10
)
--
set @id = '10010001'
select
@id
=
'
10010001
'
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
--
-全局变量
--
-必须以@@开头
--
IF ELSE
declare
@x
int
@y
int
@z
int
select
@x
=
1
@y
=
2
@z
=
3
if
@x
>
@y
print
'
x > y
'
--
打印字符串'x > y'
else
if
@y
>
@z
print
'
y > z
'
else
print
'
z > y
'
--
CASE
use
pangu
update
employee
set
e_wage
=
case
when
job_level
=
’
1
’
then
e_wage
*
1.08
when
job_level
=
’
2
’
then
e_wage
*
1.07
when
job_level
=
’
3
’
then
e_wage
*
1.06
else
e_wage
*
1.05
end
--
WHILE CONTINUE BREAK
declare
@x
int
@y
int
@c
int
select
@x
=
1
@y
=
1
while
@x
<
3
begin
print
@x
--
打印变量x 的值
while
@y
<
3
begin
select
@c
=
100
*
@x
+
@y
print
@c
--
打印变量c 的值
select
@y
=
@y
+
1
end
select
@x
=
@x
+
1
select
@y
=
1
end
--
WAITFOR
--
例 等待1 小时2 分零3 秒后才执行SELECT 语句
waitfor
delay ’
01
:
02
:
03
’
select
*
from
employee
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
--
例 等到晚上11 点零8 分后才执行SELECT 语句
waitfor
time ’
23
:
08
:
00
’
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
SELECT
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
select
*
(列名)
from
table_name(表名)
where
column_name operator value ex宿主)
select
*
from
stock_information
where
stockid
=
str
(nid)
stockname
=
'
str_name
'
stockname
like
'
% find this %
'
stockname
like
'
[a-zA-Z]%
'
--
------- ([]指定值的范围)
stockname
like
'
[^F-M]%
'
--
------- (^排除指定范围)
--
------- 只能在使用like关键字的where子句中使用通配符)
or
stockpath
=
'
stock_path
'
or
stocknumber
<
1000
and
stockindex
=
24
not
stocksex
=
'
man
'
stocknumber
between
20
and
100
stocknumber
in
(
10
,
20
,
30
)
order
by
stockid
desc
(
asc
)
--
------- 排序,desc-降序,asc-升序
order
by
1
,
2
--
------- by列号
stockname
=
(
select
stockname
from
stock_information
where
stockid
=
4
)
--
------- 子查询
--
------- 除非能确保内层select只返回一个行的值
--
------- 否则应在外层where子句中用一个in限定符
select
distinct
column_name form table_name
--
------- distinct指定检索独有的列值,不重复
select
stocknumber ,"stocknumber
+
10
"
=
stocknumber
+
10
from
table_name
select
stockname , "stocknumber"
=
count
(
*
)
from
table_name
group
by
stockname
--
------- group by 将表按行分组,指定列中有相同的值
having
count
(
*
)
=
2
--
------- having选定指定的组
select
*
from
table1, table2
where
table1.id
*=
table2.id
--
------ 左外部连接,table1中有的而table2中没有得以null表示
table1.id
=*
table2.id
--
------ 右外部连接
select
stockname
from
table1
union
[
all
]
--
------ union合并查询结果集,all-保留重复行
select
stockname
from
table2
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
insert
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
insert
into
table_name (Stock_name,Stock_number) value ("xxx","xxxx"
value (
select
Stockname , Stocknumber
from
Stock_table2)
--
-----value为select语句
update
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
update
table_name
set
Stockname
=
"xxx"
[
where Stockid = 3
]
Stockname
=
default
Stockname
=
null
Stocknumber
=
Stockname
+
4
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
delete
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
delete
from
table_name
where
Stockid
=
3
truncate
table_name
--
------- 删除表中所有行,仍保持表的完整性
drop
table
table_name
--
------- 完全删除表
alter
table
--
------ 修改数据库表结构
alter
table
database
.owner.table_name
add
column_name
char
(
2
)
null
..
sp_help table_name
--
------ 显示表已有特征
create
table
table_name (name
char
(
20
), age
smallint
, lname
varchar
(
30
))
insert
into
table_name
select
--
------ 实现删除列的方法(创建新表)
alter
table
table_name
drop
constraint
Stockname_default
--
------- 删除Stockname的default约束
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
常用函数(
function
)
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
转换函数
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
convert
(数据类型,值,格式)
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
统计函数
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
AVG
--
求平均值
COUNT
--
统计数目
MAX
--
求最大值
MIN
--
求最小值
SUM
--
求和
AVG
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
use
pangu
select
avg
(e_wage)
as
dept_avgWage
from
employee
group
by
dept_id
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
MAX
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
--
求工资最高的员工姓名
use
pangu
select
e_name
from
employee
where
e_wage
=
(
select
max
(e_wage)
from
employee)
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
STDEV
()
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
--
STDEV()函数返回表达式中所有数据的标准差
--
STDEVP()
--
STDEVP()函数返回总体标准差
VAR
()
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
--
VAR()函数返回表达式中所有值的统计变异数
VARP
()
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
--
VARP()函数返回总体变异数
算术函数
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
三角函数
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
SIN
(float_expression)
--
返回以弧度表示的角的正弦
COS
(float_expression)
--
返回以弧度表示的角的余弦
TAN
(float_expression)
--
返回以弧度表示的角的正切
COT
(float_expression)
--
返回以弧度表示的角的余切
反三角函数
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
ASIN
(float_expression)
--
返回正弦是FLOAT 值的以弧度表示的角
ACOS
(float_expression)
--
返回余弦是FLOAT 值的以弧度表示的角
ATAN
(float_expression)
--
返回正切是FLOAT 值的以弧度表示的角
ATAN2(float_expression1,float_expression2)
--
----返回正切是float_expression1 /float_expres-sion2的以弧度表示的角
DEGREES
(numeric_expression)
--
----把弧度转换为角度返回与表达式相同的数据类型可为
--
----INTEGER/MONEY/REAL/FLOAT 类型
RADIANS
(numeric_expression)
--
----把角度转换为弧度返回与表达式相同的数据类型可为
--
----INTEGER/MONEY/REAL/FLOAT 类型
EXP
(float_expression)
--
返回表达式的指数值
LOG
(float_expression)
--
返回表达式的自然对数值
LOG10
(float_expression)
--
返回表达式的以10 为底的对数值
SQRT
(float_expression)
--
返回表达式的平方根
取近似值函数
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
CEILING
(numeric_expression)
--
-----返回>=表达式的最小整数返回的数据类型与表达式相同可为
--
-----INTEGER/MONEY/REAL/FLOAT 类型
FLOOR
(numeric_expression)
--
-----返回<=表达式的最小整数返回的数据类型与表达式相同可为
--
-----INTEGER/MONEY/REAL/FLOAT 类型
ROUND
(numeric_expression)
--
-----返回以integer_expression 为精度的四舍五入值返回的数据
--
-----类型与表达式相同可为INTEGER/MONEY/REAL/FLOAT 类型
ABS
(numeric_expression)
--
-----返回表达式的绝对值返回的数据类型与表达式相同可为
--
-----INTEGER/MONEY/REAL/FLOAT 类型
SIGN
(numeric_expression)
--
-----测试参数的正负号返回0 零值1 正数或-1 负数返回的数据类型
--
-----与表达式相同可为INTEGER/MONEY/REAL/FLOAT 类型
PI
()
--
-----返回值为π 即3.1415926535897936
RAND
(
[
integer_expression
]
)
--
-----用任选的[integer_expression]做种子值得出0-1 间的随机浮点数
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
字符串函数
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
ASCII
()
--
----函数返回字符表达式最左端字符的ASCII 码值
CHAR
()
--
----函数用于将ASCII 码转换为字符
--
----如果没有输入0 ~ 255 之间的ASCII 码值CHAR 函数会返回一个NULL 值
LOWER
()
--
----函数把字符串全部转换为小写
UPPER
()
--
----函数把字符串全部转换为大写
STR
()
--
----函数把数值型数据转换为字符型数据
LTRIM
()
--
----函数把字符串头部的空格去掉
RTRIM
()
--
----函数把字符串尾部的空格去掉
LEFT
(),
RIGHT
(),
SUBSTRING
()
--
函数返回部分字符串
CHARINDEX
(),
PATINDEX
()
--
函数返回字符串中某个指定的子串出现的开始位置
SOUNDEX
()
--
----函数返回一个四位字符码
--
----SOUNDEX函数可用来查找声音相似的字符串但SOUNDEX函数对数字和汉字均只返回0 值
DIFFERENCE
()
--
----函数返回由SOUNDEX 函数返回的两个字符表达式的值的差异
--
----0 两个SOUNDEX 函数返回值的第一个字符不同
--
----1 两个SOUNDEX 函数返回值的第一个字符相同
--
----2 两个SOUNDEX 函数返回值的第一二个字符相同
--
----3 两个SOUNDEX 函数返回值的第一二三个字符相同
--
----4 两个SOUNDEX 函数返回值完全相同同
QUOTENAME
()
--
----函数返回被特定字符括起来的字符串
/**/
/*select quotename('abc', '{') quotename('abc')
运行结果如下
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
{
{abc} [abc]*/
REPLICATE
()
--
----函数返回一个重复character_expression 指定次数的字符串
/**/
/*select replicate('abc', 3) replicate( 'abc', -2)
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
运行结果如下
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
abcabcabc NULL*/
REVERSE
()
--
----函数将指定的字符串的字符排列顺序颠倒
REPLACE
()
--
----函数返回被替换了指定子串的字符串
/**/
/*select replace('abc123g', '123', 'def')
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
运行结果如下
abcdefg*/
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
SPACE
()
--
----函数返回一个有指定长度的空白字符串
STUFF
()
--
----函数用另一子串替换字符串指定位置长度的子串
数据类型转换函数
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
CAST
() 函数语法如下
CAST
() (
AS
[
length
]
)
CONVERT
() 函数语法如下
CONVERT
() (
[
length
]
,
[
, style
]
)
select
cast
(
100
+
99
as
char
)
convert
(
varchar
(
12
),
getdate
())
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
运行结果如下
199
Jan
15
2000
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
日期函数
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
DAY
()
--
----函数返回date_expression 中的日期值
MONTH
()
--
----函数返回date_expression 中的月份值
YEAR
()
--
----函数返回date_expression 中的年份值
DATEADD
( , ,)
--
---函数返回指定日期date 加上指定的额外日期间隔number 产生的新日期
DATEDIFF
( , ,)
--
---函数返回两个指定日期在datepart 方面的不同之处
DATENAME
( ,
--
----函数以字符串的形式返回日期的指定部分
DATEPART
( ,
--
----函数以整数值的形式返回日期的指定部分
GETDATE
()
--
----函数以DATETIME 的缺省格式返回系统当前的日期和时间
系统函数
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
APP_NAME
()
--
----函数返回当前执行的应用程序的名称
COALESCE
()
--
---函数返回众多表达式中第一个非NULL 表达式的值
COL_LENGTH
(
<
'
table_name
'
>
,
<
'
column_name
'
>
--
--函数返回表中指定字段的长度值
COL_NAME
(,
--
--函数返回表中指定字段的名称即列名
DATALENGTH
()
--
---函数返回数据表达式的数据的实际长度
DB_ID
(
[
'database_name'
]
)
--
----函数返回数据库的编号
DB_NAME
(database_id)
--
----函数返回数据库的名称
HOST_ID
()
--
---函数返回服务器端计算机的名称
HOST_NAME
()
--
---函数返回服务器端计算机的名称
IDENTITY
(
[
, seed increment
]
)
[
AS column_name
]
)
--
IDENTITY() 函数只在SELECT INTO 语句中使用用于插入一个identity column列到新表中
/**/
/*select identity(int, 1, 1) as column_name
into newtable
from oldtable*/
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
ISDATE
()
--
--函数判断所给定的表达式是否为合理日期
ISNULL
(,
--
函数将表达式中的NULL 值用指定值替换
ISNUMERIC
()
--
--函数判断所给定的表达式是否为合理的数值
NEWID
()
--
--函数返回一个UNIQUEIDENTIFIER 类型的数值
NULLIF
(,
--
--NULLIF 函数在expression1 与expression2 相等时返回NULL 值若不相等时则返回xpression1 的值
*******************
Transact_SQL
********************
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
--
语 句 功 能
--
数据操作
SELECT
--
从数据库表中检索数据行和列
INSERT
--
向数据库表添加新数据行
DELETE
--
从数据库表中删除数据行
UPDATE
--
更新数据库表中的数据
--
数据定义
CREATE
TABLE
--
创建一个数据库表
DROP
TABLE
--
从数据库中删除表
ALTER
TABLE
--
修改数据库表结构
CREATE
VIEW
--
创建一个视图
DROP
VIEW
--
从数据库中删除视图
CREATE
INDEX
--
为数据库表创建一个索引
DROP
INDEX
--
从数据库中删除索引
CREATE
PROCEDURE
--
创建一个存储过程
DROP
PROCEDURE
--
从数据库中删除存储过程
CREATE
TRIGGER
--
创建一个触发器
DROP
TRIGGER
--
从数据库中删除触发器
CREATE
SCHEMA
--
向数据库添加一个新模式
DROP
SCHEMA
--
从数据库中删除一个模式
CREATE
DOMAIN
--
创建一个数据值域
ALTER
DOMAIN
--
改变域定义
DROP
DOMAIN
--
从数据库中删除一个域
--
数据控制
GRANT
--
授予用户访问权限
DENY
--
拒绝用户访问
REVOKE
--
解除用户访问权限
--
事务控制
COMMIT
--
结束当前事务
ROLLBACK
--
中止当前事务
SET
TRANSACTION
--
定义当前事务数据访问特征
--
程序化SQL
DECLARE
--
为查询设定游标
EXPLAN
--
为查询描述数据访问计划
OPEN
--
检索查询结果打开一个游标
FETCH
--
检索一行查询结果
CLOSE
--
关闭游标
PREPARE
--
为动态执行准备SQL 语句
EXECUTE
--
动态地执行SQL 语句
DESCRIBE
--
描述准备好的查询
--
-局部变量
declare
@id
char
(
10
)
--
set @id = '10010001'
select
@id
=
'
10010001
'
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
--
-全局变量
--
-必须以@@开头
--
IF ELSE
declare
@x
int
@y
int
@z
int
select
@x
=
1
@y
=
2
@z
=
3
if
@x
>
@y
print
'
x > y
'
--
打印字符串'x > y'
else
if
@y
>
@z
print
'
y > z
'
else
print
'
z > y
'
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
--
CASE
use
pangu
update
employee
set
e_wage
=
case
when
job_level
=
’
1
’
then
e_wage
*
1.08
when
job_level
=
’
2
’
then
e_wage
*
1.07
when
job_level
=
’
3
’
then
e_wage
*
1.06
else
e_wage
*
1.05
end
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
--
WHILE CONTINUE BREAK
declare
@x
int
@y
int
@c
int
select
@x
=
1
@y
=
1
while
@x
<
3
begin
print
@x
--
打印变量x 的值
while
@y
<
3
begin
select
@c
=
100
*
@x
+
@y
print
@c
--
打印变量c 的值
select
@y
=
@y
+
1
end
select
@x
=
@x
+
1
select
@y
=
1
end
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
--
WAITFOR
--
例 等待1 小时2 分零3 秒后才执行SELECT 语句
waitfor
delay ’
01
:
02
:
03
’
select
*
from
employee
--
例 等到晚上11 点零8 分后才执行SELECT 语句
waitfor
time ’
23
:
08
:
00
’
select
*
from
employee
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
***
SELECT
***
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
select
*
(列名)
from
table_name(表名)
where
column_name operator value
ex:(宿主)
select
*
from
stock_information
where
stockid
=
str
(nid)
stockname
=
'
str_name
'
stockname
like
'
% find this %
'
stockname
like
'
[a-zA-Z]%
'
--
------- ([]指定值的范围)
stockname
like
'
[^F-M]%
'
--
------- (^排除指定范围)
--
------- 只能在使用like关键字的where子句中使用通配符)
or
stockpath
=
'
stock_path
'
or
stocknumber
<
1000
and
stockindex
=
24
not
stocksex
=
'
man
'
stocknumber
between
20
and
100
stocknumber
in
(
10
,
20
,
30
)
order
by
stockid
desc
(
asc
)
--
------- 排序,desc-降序,asc-升序
order
by
1
,
2
--
------- by列号
stockname
=
(
select
stockname
from
stock_information
where
stockid
=
4
)
--
------- 子查询
--
------- 除非能确保内层select只返回一个行的值,
--
------- 否则应在外层where子句中用一个in限定符
select
distinct
column_name form table_name
--
------- distinct指定检索独有的列值,不重复
select
stocknumber ,"stocknumber
+
10
"
=
stocknumber
+
10
from
table_name
select
stockname , "stocknumber"
=
count
(
*
)
from
table_name
group
by
stockname
--
------- group by 将表按行分组,指定列中有相同的值
having
count
(
*
)
=
2
--
------- having选定指定的组
select
*
from
table1, table2
where
table1.id
*=
table2.id
--
------ 左外部连接,table1中有的而table2中没有得以null表示
table1.id
=*
table2.id
--
------ 右外部连接
select
stockname
from
table1
union
[
all
]
--
--- union合并查询结果集,all-保留重复行
select
stockname
from
table2
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
***
insert
***
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
insert
into
table_name (Stock_name,Stock_number) value ("xxx","xxxx")
value (
select
Stockname , Stocknumber
from
Stock_table2)
--
-value为select语句
***
update
***
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
update
table_name
set
Stockname
=
"xxx"
[
where Stockid = 3
]
Stockname
=
default
Stockname
=
null
Stocknumber
=
Stockname
+
4
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
***
delete
***
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
delete
from
table_name
where
Stockid
=
3
truncate
table_name
--
--------- 删除表中所有行,仍保持表的完整性
drop
table
table_name
--
------------- 完全删除表
***
alter
table
***
--
- 修改数据库表结构
alter
table
database
.owner.table_name
add
column_name
char
(
2
)
null
..
sp_help table_name
--
-- 显示表已有特征
create
table
table_name (name
char
(
20
), age
smallint
, lname
varchar
(
30
))
insert
into
table_name
select
![](http://ghd258.cnblogs.com/Images/dot.gif)
![](http://ghd258.cnblogs.com/Images/dot.gif)
--
--- 实现删除列的方法(创建新表)
alter
table
table_name
drop
constraint
Stockname_default
--
-- 删除Stockname的default约束
***
function
(
/*常用函数*/
)
***
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
--
--统计函数----
AVG
--
求平均值
COUNT
--
统计数目
MAX
--
求最大值
MIN
--
求最小值
SUM
--
求和
--
AVG
use
pangu
select
avg
(e_wage)
as
dept_avgWage
from
employee
group
by
dept_id
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
--
MAX
--
求工资最高的员工姓名
use
pangu
select
e_name
from
employee
where
e_wage
=
(
select
max
(e_wage)
from
employee)
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
--
STDEV()
--
STDEV()函数返回表达式中所有数据的标准差
--
STDEVP()
--
STDEVP()函数返回总体标准差
--
VAR()
--
VAR()函数返回表达式中所有值的统计变异数
--
VARP()
--
VARP()函数返回总体变异数
--
--算术函数----
/***三角函数***/
SIN
(float_expression)
--
返回以弧度表示的角的正弦
COS
(float_expression)
--
返回以弧度表示的角的余弦
TAN
(float_expression)
--
返回以弧度表示的角的正切
COT
(float_expression)
--
返回以弧度表示的角的余切
/***反三角函数***/
ASIN
(float_expression)
--
返回正弦是FLOAT 值的以弧度表示的角
ACOS
(float_expression)
--
返回余弦是FLOAT 值的以弧度表示的角
ATAN
(float_expression)
--
返回正切是FLOAT 值的以弧度表示的角
ATAN2(float_expression1,float_expression2)
--
返回正切是float_expression1 /float_expres-sion2的以弧度表示的角
DEGREES
(numeric_expression)
--
把弧度转换为角度返回与表达式相同的数据类型可为
--
INTEGER/MONEY/REAL/FLOAT 类型
RADIANS
(numeric_expression)
--
把角度转换为弧度返回与表达式相同的数据类型可为
--
INTEGER/MONEY/REAL/FLOAT 类型
EXP
(float_expression)
--
返回表达式的指数值
LOG
(float_expression)
--
返回表达式的自然对数值
LOG10
(float_expression)
--
返回表达式的以10 为底的对数值
SQRT
(float_expression)
--
返回表达式的平方根
/***取近似值函数***/
CEILING
(numeric_expression)
--
返回>=表达式的最小整数返回的数据类型与表达式相同可为
--
INTEGER/MONEY/REAL/FLOAT 类型
FLOOR
(numeric_expression)
--
返回<=表达式的最小整数返回的数据类型与表达式相同可为
--
INTEGER/MONEY/REAL/FLOAT 类型
ROUND
(numeric_expression)
--
返回以integer_expression 为精度的四舍五入值返回的数据
--
类型与表达式相同可为INTEGER/MONEY/REAL/FLOAT 类型
ABS
(numeric_expression)
--
返回表达式的绝对值返回的数据类型与表达式相同可为
--
INTEGER/MONEY/REAL/FLOAT 类型
SIGN
(numeric_expression)
--
测试参数的正负号返回0 零值1 正数或-1 负数返回的数据类型
--
与表达式相同可为INTEGER/MONEY/REAL/FLOAT 类型
PI
()
--
返回值为π 即3.1415926535897936
RAND
(
[
integer_expression
]
)
--
用任选的[integer_expression]做种子值得出0-1 间的随机浮点数
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
--
--字符串函数----
ASCII
()
--
函数返回字符表达式最左端字符的ASCII 码值
CHAR
()
--
函数用于将ASCII 码转换为字符
--
如果没有输入0 ~ 255 之间的ASCII 码值CHAR 函数会返回一个NULL 值
LOWER
()
--
函数把字符串全部转换为小写
UPPER
()
--
函数把字符串全部转换为大写
STR
()
--
函数把数值型数据转换为字符型数据
LTRIM
()
--
函数把字符串头部的空格去掉
RTRIM
()
--
函数把字符串尾部的空格去掉
LEFT
(),
RIGHT
(),
SUBSTRING
()
--
函数返回部分字符串
CHARINDEX
(),
PATINDEX
()
--
函数返回字符串中某个指定的子串出现的开始位置
SOUNDEX
()
--
函数返回一个四位字符码
--
SOUNDEX函数可用来查找声音相似的字符串但SOUNDEX函数对数字和汉字均只返回0 值
DIFFERENCE
()
--
函数返回由SOUNDEX 函数返回的两个字符表达式的值的差异
--
0 两个SOUNDEX 函数返回值的第一个字符不同
--
1 两个SOUNDEX 函数返回值的第一个字符相同
--
2 两个SOUNDEX 函数返回值的第一二个字符相同
--
3 两个SOUNDEX 函数返回值的第一二三个字符相同
--
4 两个SOUNDEX 函数返回值完全相同
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
QUOTENAME
()
--
函数返回被特定字符括起来的字符串
/*select quotename('abc', '{') quotename('abc')
运行结果如下
----------------------------------{
{abc} [abc]*/
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
REPLICATE
()
--
函数返回一个重复character_expression 指定次数的字符串
/*select replicate('abc', 3) replicate( 'abc', -2)
运行结果如下
----------- -----------
abcabcabc NULL*/
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
REVERSE
()
--
函数将指定的字符串的字符排列顺序颠倒
REPLACE
()
--
函数返回被替换了指定子串的字符串
/*select replace('abc123g', '123', 'def')
运行结果如下
----------- -----------
abcdefg*/
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
SPACE
()
--
函数返回一个有指定长度的空白字符串
STUFF
()
--
函数用另一子串替换字符串指定位置长度的子串
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
--
--数据类型转换函数----
CAST
() 函数语法如下
CAST
() (
<
expression
>
AS
<
data_ type
>
[
length
]
)
CONVERT
() 函数语法如下
CONVERT
() (
<
data_ type
>
[
length
]
,
<
expression
>
[
, style
]
)
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
select
cast
(
100
+
99
as
char
)
convert
(
varchar
(
12
),
getdate
())
运行结果如下
--
---------------------------- ------------
199
Jan
15
2000
![](http://ghd258.cnblogs.com/Images/OutliningIndicators/None.gif)
--
--日期函数----
DAY
()
--
函数返回date_expression 中的日期值
MONTH
()
--
函数返回date_expression 中的月份值
YEAR
()
--
函数返回date_expression 中的年份值
DATEADD
(
<
datepart
>
,
<
number
>
,
<
date
>
)
--
函数返回指定日期date 加上指定的额外日期间隔number 产生的新日期
DATEDIFF
(
<
datepart
>
,
<
number
>
,
<
date
>
)
--
函数返回两个指定日期在datepart 方面的不同之处
DATENAME
(
<
datepart
>
,
<
date
>
)
--
函数以字符串的形式返回日期的指定部分
DATEPART
(
<
datepart
>
,
<
date
>
)
--
函数以整数值的形式返回日期的指定部分
GETDATE
()
--
函数以DATETIME 的缺省格式返回系统当前的日期和时间
--
--系统函数----
APP_NAME
()
--
函数返回当前执行的应用程序的名称
COALESCE
()
--
函数返回众多表达式中第一个非NULL 表达式的值
COL_LENGTH
(
<
'
table_name
'
>
,
<
'
column_name
'
>
)
--
函数返回表中指定字段的长度值
COL_NAME
(
<
table_id
>
,
<
column_id
>
)
--
函数返回表中指定字段的名称即列名
DATALENGTH
()
--
函数返回数据表达式的数据的实际长度
DB_ID
(
[
'database_name'
]
)
--
函数返回数据库的编号
DB_NAME
(database_id)
--
函数返回数据库的名称
HOST_ID
()
--
函数返回服务器端计算机的名称
HOST_NAME
()
--
函数返回服务器端计算机的名称
IDENTITY
(
<
data_type
>
[
, seed increment
]
)
[
AS column_name
]
)
--
IDENTITY() 函数只在SELECT INTO 语句中使用用于插入一个identity column列到新表中
/*select identity(int, 1, 1) as column_name
into newtable
from oldtable*/
ISDATE
()
--
函数判断所给定的表达式是否为合理日期
ISNULL
(
<
check_expression
>
,
<
replacement_value
>
)
--
函数将表达式中的NULL 值用指定值替换
ISNUMERIC
()
--
函数判断所给定的表达式是否为合理的数值
NEWID
()
--
函数返回一个UNIQUEIDENTIFIER 类型的数值
NULLIF
(
<
expression1
>
,
<
expression2
>
)
--
NULLIF 函数在expression1 与expression2 相等时返回NULL 值若不相等时则返回expression1 的值