现在的位置: 首页 > 综合 > 正文

SQL学习

2013年10月21日 ⁄ 综合 ⁄ 共 1110字 ⁄ 字号 评论关闭

一:用户定义函数

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

 

抱歉!评论已关闭.