在线咨询
QQ咨询
服务热线
服务热线:13125520620
TOP

SQL学习:用户定义函数-数据库

发布时间:2011-11-12 浏览:4795

    三种类型: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属性和所有其他都能被放置在表变量上。可以进行例似于表的操作,但没有被存储在磁盘中,可作为变量传递。

TAG
软件定制,软件开发,瀚森HANSEN
0
该内容对我有帮助