You are on page 1of 26

SQL Server 2000

2005 by Prentice Hall

//

SQL Server 2000

SQL Server

SQL Sever(Getdate()
Convert())

SQL Server 2000

1)
2)
3)

SQL Server 2000

//

T-SQL

Create Function <function_name> (@parameter


datatype [ , n] )
Return <return_datatype> AS
Begin
function_body
Return { <value> | <@variable> }
End

SQL Server 2000

17-1
--,
--T-SQL

CREATE FUNCTION dbo.fn_Rect_Area (@Len int ,@Width int )


RETURNS int AS

BEGIN

DECLARE @Area int

SET @Area = @Len * @Width


RETURN @Area
END

SQL Server 2000

SQL Server 2000

ALTER FUNCTION

ALTER FUNCTIONCREATE FUNCTION


CREATEALTER

ALTER FUNCTION

Enterprise Manager

SQL Server 2000

SQL Server 2000

SQL Server 2000

10

1. DROP FUNCTION
DROP FUNCTION { [ owner_name.] function_name } [ ,...n ]

DROP FUNCTION Eric.fn_pretax, dbo.fn_Date

2. Enterprise Manager

SQL Server 2000

11

SQL Server 2000

12

SQL Server

sysadmin db_owner db_ddladmin


CREATE FUNCTION

sysadmin db_owner GRANT


CREATE FUNCTION

CREATE FUNCTION CREATE FUNCTION

EXECUTE
EXECUTE

CONSTRAINTDEFAULT
REFERENCES

SQL Server 2000

13

SQL Server 2000

14

SQL Server 2000

15


CREATE FUNCTION [ owner_name.] function_name
( [ { @parameter_name scalar_parameter_data_type [ = default ] } [ ,...n ] ] )
RETURNS scalar_return_data_type
[ WITH < function_option> [ [,] ...n] ]
[ AS ]
BEGIN
function_body
RETURN scalar_expression
END

RETURN

SQL Server 2000

16

17-3
USE
GO
--
CREATE FUNCTION dbo.fn_Past_Service
(@hireday datetime,
@current datetime
)
RETURNS int
AS
BEGIN
DECLARE @MPast int
SET @MPast = DATEDIFF(yy,@hireday,@current)
RETURN @Mpast
END
GO
--
USE
GO
SELECT ,,dbo.fn_Past_Service(,getdate()) AS
FROM ORDER BY 3 DESC
GO

SQL Server 2000

(30)

17


CREATE FUNCTION [ owner_name.] function_name
( [ { @parameter_name scalar_parameter_data_type [ = default ] } [ ,...n ] ] )
RETURNS TABLE
[ WITH < function_option > [ [,] ...n ] ]
[ AS ]
RETURN [ ( ) select-stmt [ ] ]

SELECTFROM
T-SQL

SELECT

RETURNSELECT

(Index
View)

SQL Server 2000

18

17-4
USE
GO
CREATE FUNCTION dbo.fn_QuerySalary(@ money,@ money)
RETURNS TABLE
RETURN ( SELECT ,, FROM
WHERE >= @ AND <= @)
GO
----3000040000
SELECT * FROM fn_QuerySalary(30000,40000) ORDER BY DESC

(15)

SQL Server 2000

19


CREATE FUNCTION [ owner_name.] function_name
( [ { @parameter_name 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 ] )

T-SQL
View

RETURNS @return_variable TABLE < table_type_definition > table


RETURNtable
SQL Server 2000

20

17-6
USE
GO
CREATE FUNCTION dbo.fn_Dept_Service
( @MDept varchar(10),
@current datetime,
@MStart_year int,
@MEnd_year int )
RETURNS @MPersonTbl TABLE ( nvarchar(10) ,
nvarchar(10) ,
int)
AS
BEGIN
-- table @MPersonTbl
INSERT @MPersonTbl
SELECT ,,=DATEDIFF(yy,,@current)
FROM
WHERE DATEDIFF(yy,,@current) BETWEEN
@MStart_year AND @MEnd_year AND = @MDept
RETURN
END
GO
-- fn_Dept_Service
SELECT * FROM dbo.fn_Dept_Service('','2004/3/29',5,10)

SQL Server 2000

21

(
dbo.GetSalary)

(..)

SQL Server 2000

22

17-8
--
DECLARE @Area int
SET @Area = dbo.fn_Rect_Area(10,2) --
SELECT = @Area
--
--
USE
GO
SELECT ,dbo.fn_Past_Service(,getdate()) AS
FROM

(30)

DEFAULT

SQL Server 2000

23


sp_rename [ @objname = ] 'object_name' ,
[@newname =] 'new_name'
[ , [ @objtype = ] 'object_type' ]

sp_rename

17-10 fn_Area_Area()(OBJECT)fn_Area()
EXEC sp_rename 'fn_Rect_Area' , 'fn_Area' , 'OBJECT'

SQL Server 2000

24

DECLARE

SETSELECT

FETCH

IfWhile

tableInsertDeleteUpdate

EXECUTESQL

SQL Server 2000

25

SQL Server 2000

26