程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> SqlServer數據庫 >> 關於SqlServer >> SQLSERVER2000新增功能介紹1

SQLSERVER2000新增功能介紹1

編輯:關於SqlServer

建立用戶自定義函數,可以自由的在一個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')

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved