存储过程和用户自定义函数

存储过程和用户自定义函数

ID:38677743

大小:34.00 KB

页数:3页

时间:2019-06-17

存储过程和用户自定义函数_第1页
存储过程和用户自定义函数_第2页
存储过程和用户自定义函数_第3页
资源描述:

《存储过程和用户自定义函数》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、存储过程和用户自定义函数一:存储过程的简单创建,修改与删除1.创建简单的存储过程use AdventureWorksgocreate proc spEmployeeasselect * from HumanResources.Employee执行上面的代码就创建了一个存储过程如果想运行这个存储过程可以直接执行execspEmployee这个语句2.更改存储过程ALTER proc [dbo].[spEmployee]asselect top 13 * from HumanResources.Employee3

2、.删除存储过程drop proc dbo.spEmployee二:存储过程的输入参数和输出参数1.有输入参数的存储过程use AdventureWorksgocreate proc spEmployee    @LastName nvarchar(50) = nullasif @LastName is null    select top 13 * from HumanResources.Employeeelse    select top 10 * from HumanResources.Employee查

3、看该存储过程的结果可以用execspEmployee'123'或直接execspEmployee存储过程的重载...2.有输出参数的存储过程use AdventureWorksgoalter proc spEmployee    @LastName nvarchar(50) = null outputasif @LastName is null    begin        print 'null'        return '123'    endelse    begin        print @

4、LastName        return '456'    end看第一个测试该存储过程的语句declare @myval nvarchar(50)exec @myval = spEmployee @myval outputprint @myval输出null 123第二个测试该存储过程的语句declare @myval nvarchar(50)set @myval = 'xland'exec @myval = spEmployee @myval outputprint @myval输出xland 456

5、三:用户定义函数1.返回标量值的用户定义函数先做一个简单的日期处理函数把长日期缩短成短日期Create function dbo.DayOnly(@date datetime)returns varchar(12)asbegin     return convert(varchar(12),@date,101)end为了测试上面的函数先做个脚本use Accountingdeclare @counter intset @counter = 1while @counter <= 10begin    inser

6、t into Orders values(1,dateadd(mi,@counter,getdate()),1)    set @counter = @counter +1end 然后检索这个脚本新插入的数据记录use Accountingselect * from orders where dbo.DayOnly(date1) = dbo.DayOnly(getdate())2.返回表的用户定义函数先看例子use AdventureWorksgocreate function dbo.fnContactSe

7、arch(@LastName nvarchar(50))returns tableasreturn (select * from Person.Contact where LastName like @LastName+'%')执行这个例子use AdventureWorksselect * from fnContactSearch('Ad')3.综合例子:返回表,有输入参数use xlandgocreate function dbo.funGetMytable    (@id as int)    retu

8、rns @allrows table    (        id  int not null,        title  nvarchar(max) null    )asbegin    insert into @allrows  select id,title from mytable where id = @idreturnend go执行这个例子select * from funGetM

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。