SQLSERVER2000新增功能介绍1
2007-11-11 08:41:27 来源:WEB开发网建立用户自定义函数,可以自由的在一个Transaction SQL 中直接使用,就像一个系统函数一样在你的查询语句中
直接使用。
CREATE FUNCTION
语法
CREATE FUNCTION [ owner_name. ] function_name
( [ { @parameter_name [AS] scalar_parameter_data_type [ = default ] } [ ,...n ] ] )
RETURNS scalar_return_data_type
[ WITH < function_option> [ [,] ...n] ]
[ AS ]
BEGIN
function_body
RETURN scalar_expression
END
Inline Table-valued Functions
CREATE FUNCTION [ owner_name. ] function_name
( [ { @parameter_name [AS] scalar_parameter_data_type [ = default ] } [ ,...n ] ] )
RETURNS TABLE
[ WITH < function_option > [ [,] ...n ] ]
[ AS ]
RETURN [ ( ] select-stmt [ ) ]
Multi-statement Table-valued Functions
CREATE FUNCTION [ owner_name. ] function_name
( [ { @parameter_name [AS] scalar_parameter_data_type [ = default ] } [ ,...n ] ] )
RETURNS @return_variable TABLE < table_type_definition >
[ WITH < function_option > [ [,] ...n ] ]
[ AS ]
BEGIN
function_body
RETURN
END
< function_option > ::=
{ ENCRYPTION | SCHEMABINDING }
< table_type_definition > :: =
( { column_definition | table_constraint } [ ,...n ] )
示例
--创建函数
create function getValue()
returns int
as
begin
return (select convert(int,Value) AS Value from Test where [Name]='Test')
end
go
select * from Test2 where Value=dbo.getValue()
go
create function getTabletest(@t1 datetime,@t2 datetime)
returns table
as
return( select t1.Field1 ,t2.Field1,t2.Qty,t3.Field1,t3.Field1
from Table1 t1 inner join Table2 t2 on t1.ID=t2.ID inner join Table3 t3 on t2.JID=t3.JID where t1.Date>=@t1 and t1.Date<@t2)
go
select * from dbo.getTabletest('1999/1/1','2001/2/1')
- ››sqlserver 每30分自动生成一次
- ››sqlserver安装和简单的使用
- ››SqlServer触发器、存储过程和函数
- ››SQLServer建立交叉表查询
- ››SqlServer强制断开数据库连接的方法
- ››SQLServer 2005 海量数据解决方案
- ››SQLServer 2008数据库查看死锁、堵塞的SQL语句
- ››SqlServer 插入多条数据
- ››SQLServer 2008 R2导出的SQL文件中没有数据
- ››功能强更实用 Win7库变为“计算机”
- ››SQLSERVER通过游标查询两个数据表共有字段名组合成...
- ››sqlserver2008实现拼音首字母和随机n位数的生成
赞助商链接