sql函数集合Word文档下载推荐.docx
《sql函数集合Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《sql函数集合Word文档下载推荐.docx(64页珍藏版)》请在冰点文库上搜索。
>
tablebc.bchecksum)//标志出变化的行
3.CHECKSUM返回在表的行上或在表达式上计算的校验值CHECKSUM用于生成
哈希索引
Setarithaborton
Usenorthwind
Altertableproducts
Addcs_pnameaschecksum(productname)//在参数列是添加一个校验值
Createimdexpname_indexonproducts(cs_pname)//生成索引
Go
Selecttop5cs_pnamefromproductsorderbycs_pnamedesc//选择根据索引
列的前5个cs_pname
4.Checksum_agg返回组中值的校验值。
空值冽被忽略。
例如:
Usenorthwind
Go
Selectchecksum_agg(cast(unitsinstockasint))//检测products表的unitsinstock列
的更改
fromproducts
5.Count返回组中项目的数量
例一:
Usepubs
Selectcount(distinctcity)//对city中的每一行都计算,并返回非空的数量
Fromauthors
例二:
Usepubs
Selectcount(*)//返回组中项目的数量
Fromtitles
例三:
Selectcount(*),avg(price)//选择advance大于$1000的总数和平均price
Fromtitles
Whereadvance>
$1000
6.Count_big返回组中项目的数量。
在使用上和count基本上是一样的,只是在返回值上
有一点区别,count_big返回是bigint的数据类型值,count返回的是int数据
类型值。
7.Grouping返回一个聚合函数,它产生一个附加列,当用CUBE或ROLLUP运算符添加
行时,附加的列输出值为1,当所添加的行不是由CUBE或ROLLUP产生
时,附加列值为0
Selectroyalty,sum(advance)‘totaladvance’,//选择列royalty,聚合advance数值
Grouping(royalty)‘grp’//grouping函数
Fromtitles
Groupbyroyaltywithrollup//groupby与rollup相联系产生分组
8.Max返回表达式的最大值
Selectmax(ytd_sales)//选择ytd_sales列属性,求其最大值。
9.Min返回表达式的最小值
Usepubs
Selectmin(ytd_sales)//选择ytd_sales列属性,求其最小值。
10.Stdev返回给定表达式中所有值的统计标准偏差。
Selectstdev(royalty)
11.Stdevp返回给定表达式中所有值的填充统计标准偏差。
12.sum返回表达式中所有值的的和,或只返回DISTINCT值。
SUM只能用数字列。
Selecttype,sum(price),sum(advance)//选择type,price的和,advance的和
Wheretypelike‘%cook’//匹配结尾字符为cook
Groupbytype
Orderbytype
例如2:
Go
Selecttype,price,advance
Wheretypelike’%cook’
Computesum(price),sum(advance)bytype//根据type的属性计算price和advance
的和
13.Var返回给定表达式中所有值的统计方差。
Selecdtvar(royalty)
14.Varp返回给定表达式中所有值的填充的统计方差
Selectvarp(royalty)
时间及日期函数TOP
1.Dateadd在向指定日期加上一段时间的基础上返回新datetime值。
Selectdateadd(day,3,Hiredate)//显示函数dateadd执行结果
Fromemployees
2.datediff返回跨两个指定日期和时间边界数。
Selectdatediff(day,Hiredate,getdate())asno_of_days
go
3.Datename返回代表指定日期的指定日期部分的字符串。
Selectdatename(month,getdate())as‘monthname’
4.Datepart返回代表指定日期的指定日期部分的整数。
Selectdatepart(month,getdate())as‘monthnumber’
Selectdatepart(m,0),datepart(d,0),datepart(yy,0)
5.Daymonthyear返回指定日期的天月年的日期部分的整数
Selectmonth(0),day(0),year(0)
6.Getdate按datetime值的标准内部格式返回当前系统时间和日期
Createtablesales//创建sales表
(
Sale_idchar(11)notnull//sale_id列,类型char(11),notnull
Sale_namevarchar(40)notnull//sale_name列,类型varchar(40),notnull
Sale_datedatetimedefaullgetdate()//sale_date列,类型datetime,默认值getdate()
)
Insertintosales(sale_id,sale_name)
Values(1,’foods’)//向表内插值
Select*fromsales//表结果检索
7.Getutcdate返回表当前CUT时间的datetime值。
Selectgetutcdatea()
数学函数TOP
1.Abs返回给定数字的绝对值
2.Acos返回以弧度表示的角度值,该角度值的余弦为给定的float表达式,也叫反余弦
3.Asin返回以弧度表示的角度值,也叫反正弦。
Declare@anglefloat
Set@angle=-1.01
Select‘theasinoftheangkeis:
’+convert(varchar,asin(@angle))
4.Atan返回以弧度表示的角度值,该角度值的正切为给定的float表达式,也叫反正切。
5.Atn2返回以弧度表示的角度值,该角度值的正切介于两个给定的float表达式之间
Declare@anglelfloat
Declare@angle2float
Set@anglel=35.175643
Set@angle2=129.44
Select‘theatn2oftheangleis:
’+convert(varchar,atn2(@anglel,@angle2))
6.Ceiling返回或等于所给数字表达式的最小整数。
Selectceiling($123.45),ceiling($-123.45),ceiling($0.0)
7.Cos返回给定表达式中给定角度的三角余弦值
8.Cot返回给定float表达式指定角度的三角余切值
Set@angle=124.1332
Select‘thecotfotheangleis:
’+convert(varchar,cot(@angle))
9.Degrees当给出弧度为单位的角度时,返回相应的以度数为单位的角度。
Select‘thenumberofdegreesinPI/2radinansis:
’+convert(varchar,degrees((PI()/2)))
10.Exp返回所给的float表达式的指数值。
11.floor返回小于或等于所给数字表达式的最大整数。
12.log返回给定float表达式的自然对数。
13.log10返回给定float表达式的以10为底的对数。
Declare@varfloat
Set@var=5.175643
Select‘thelogofthevariableis:
’+convert(varchar,log(@var))
14.PI返回PI的常量值。
15.power返回给定表达式乘指定次方的值。
Declare@valueint,@counterint
Set@value=2
Set@counter=1
While@counter<
5
Begin
Selectpower(@value,@counter)
Setnocounton
Set@counter=@counter+1
End
16.radians对于数字表达式中输入的度数值返回弧度值。
17.rand返回0到1之间的随机float值。
18.round返回数字表达式并四舍五入为指定的长度或精度。
Selectround(123.9995,3),round(123.9994,3)
19.sign返回给定表达式的正零或负号
Declare@anglegloat
Declare@valuereal
Set@value=-1
While@value<
2
Begin
Selectsign(@value)
Setnocounton
Select@value=value+1
end
Setnocountoff
20.sin以近似数字表达式返回给定角度的三角正弦值。
21.sqrt返回给定表达式的平方根。
Declare@myvaluefloat
Set@myvalue=1.00
While@myvlaue<
10
Begin
Selectsqrt(@myvalue)
Select@myvalue=@myvalue+1
22.square返回给定表达式的平方值。
23.tan返回给定表达式的正切。
元数据函数TOP
1.col_length返回列的定义长度
Createtablet1
(c1varchar(40),
C2nvarchar(80))
Selectcol_length(‘t1’,’c1’)as‘varchar’
Selectcol_length(‘t2’,’c2’)as‘nvarchar’
Droptablet1
2.col_name返回数据库列的名称,该列具有相应的表标识号和列标识号。
Selectcol_name(object_id(‘employees’),1)asemployees
3.columnproperty返回有关列或过程的参数的信息。
Selectcolumnproperty(object_id(‘employees’),’title’,’precision’)
4.databaseproperty返回给定数据库和属性名的命名数据库属性值。
Selectdatabaseproperty(‘northwind’,’isautoclose’)
5.databasepropertyex返回指定数据库的指定数据库选项或属性的当前设置。
Go
Selectdatabasepropertyex(‘northwind’,isautoclose’)
6.db_id返回数据库标识ID
Selectname,db_id(name)asdb_id
Fromsysdatabases
Orderbydbid
7.db_name返回数据库名称。
8.file_id返回当前数据库中给定逻辑文件标识(id)号。
9.file_name返回指定文件标识(id)号的逻辑文件名。
10.filegroup_id返回给定文件组名称号
11.filegroup_name返回给定文件组标识(id)号的文件组名。
12.filegroupproperty给定文件组和属性名时,返回指定的文件组属性值。
13.fileproperty给定文件名和属性时返回指定的文件名属性值。
14.fn_listextendedproperty返回数据库对像的扩展属性值。
Createtablet1(idint,namechar(20))//创建表T1
Execsp_addextendedproperty‘caption’,’employeeid’,’user’,dbo,’table’,’t1’,
’column’,id//为表T1列ID添加扩展属性
Execsp_addextendedproperty‘caption’,’employeename’,’user’,dbo,‘table’,
‘t1’,’column’,name
Select*from:
:
fn_listextendedproperty(null,’suer’,’dbo’,table’,t1’,’column’,
Default)//列举表T1的扩展属性
15.fulltextserviceproperty返回有关全文服务级别属性的信息。
原型:
fulltextserviceproperty(catalog_name,property)
参数说明:
Catalog_name包含全文目录名称的表达式。
Property包含全文目录属性名称的表达式。
Property参数值列表
Populatestatus0=空闲1=正在进行完全填充2=已暂停
3=中止4=正在恢复5=关机
6=正在进行增量填充7=生成索引
8=磁盘已满,已暂停9=更改跟踪
Go
Selectfulltextcatalogproperty(‘cat_desc’,’itemcount”)
16.fulltextserviceproperty返回有关全文服务级别属性的信息。
fulltextserviceproperty(property)
Property参数说明
ResourceUsage
一个从1(后台)到5(专用)之间的值。
ConnectTimeout
在超时发生前,Microsoft搜索服务等待所有与Microsoft®
SQLSever™数据库服务器的连接完成以便进行全文索引填充所用的时间(以秒为单位)。
IsFulltextInstalled
在SQLServer的当前实例中安装全文组件。
1=已安装全文组件。
0=未安装全文组件。
NULL=输入无效或发生错误。
DataTimeout
在超时发生前,Microsoft搜索服务等待所有由MicrosoftSQLServer数据库服务器返回数据以便进行全文索引填充所用的时间(以秒为单位)。
Selectfulltextserviceproperty(‘isfulltextinstalled’)
17.index_col返回索引列名称。
index_col(‘table’,’index_id’,’key_id’)
参数:
table表的名称。
Index_id索引的ID
Key_id键的ID
Usenorthwind
Declare@idint,@typechar
(2),@msgvarchar(10),@indidsmallint,
@indnamesysname,@statusint//声明变量
Select@id=id,@type=type//获得employees表的ID号以便在系统索引库中
查找其索引
Fromsysobjects
Wherename=’employees’andtype=’u’
Print‘indexinformationfortheauthorstable’//打印输出信息
Print‘----------------------------------------------’
DeclareIcursor//声明一个游标
For
Selectindid,name,status//循环搜索employees表中所有的索引
Fromsysindexes
Whereid=@id
OpenI//打开游标
FetchnextfromIinto@indid,@indname,@status//获取下一系列索引信息
If@@fetch_status=0//如果状态为0,打印‘’
Print‘‘
While@@fetch_status=0//循环如果从游标处查找还有行,打印出相关
索引信息
Begin
Set@msg=null//初始化变量msg为null
Set@msg=‘indexnumber‘+convert(varchar,@indid)+
‘is’+@indname//填充索引名变量
Set@indkey=1//初始化变量indkey为1
While@indkey<
=16andindex_col(@name,@indid,
@indkey)//indkey等于key_id,其值可以从1到16
Isnot