SQL学习:用户定义函数

王朝mssql·作者佚名  2006-11-24
窄屏简体版  字體: |||超大  

三种类型:1,标量函数;2,内嵌表值函数;3,多语句表值函数。

声明:create function <function name>(<parameters>)

return<return value>

with<function option>

as

begin

<function body>

end

//内嵌表值函数 函数体被省略,不需要begin和end关键字

create function employeeDepartment (@DepartmentID int)

returns table

as

return (select * from employee where DepartmentId = @DepartmentId)

//程序中调用

SqlCommond com = new SqlCommand("select * from dbo.employeeDepartment(@DepartmentID)");

//多语句表值函数 在函数体后是return关键字,该关键字后跟函数声明中所定义的返回表变量

create function employeeByDepartment (@DepartmentID int)

returns @EmployeeList Table (Id int,LastName varchar(25),EmployeeNumber samllint, HireDate datetime)

AS

Begin

Insert into @EmployeeList Values select ID,LastName,EmployeeNumber,HireDate Employee where DepartmentId=@departmentID

return @meployeeList

end

//标量函数 return后跟需要被返回的标量值,这可以是函数体内已经定义的一个变量或者是一个常量。

附:Table数据类型

声明:Declare @TestTable table(ID int,Name varchar(20),Description varchar(200))..

check约束 默认约束 identity属性和所有其他都能被放置在表变量上。可以进行例似于表的操作,但没有被存储在磁盘中,可作为变量传递。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航