SQL SERVER 2000新增功能介绍1
2024-08-31 00:48:30
供稿:网友
建立用户自定义函数,可以自由的在一个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>[email protected] and t1.date<@t2)
go
select * from dbo.gettabletest('1999/1/1','2001/2/1')