2022年存储过程、触发器的例子( .pdf
《2022年存储过程、触发器的例子( .pdf》由会员分享,可在线阅读,更多相关《2022年存储过程、触发器的例子( .pdf(12页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、存储过程:1.CREATEPROCEDURE loving AS BEGIN SELECT*from student where ssex=女END:执行Exec loving 2.CREATEPROCEDURE loving1 as BEGIN SELECT*from student end 3.CREATEPROCEDURE pr1_sc_ins Param1 char(10),Param2 char(2),Param3 realAS BEGIN insertinto sc(sno,cno,grade)values(Param1,Param2,Param3)END 执行:EXEC pr1_
2、sc_ins 200215121,4,85 或EXEC pr1_sc_ins Param1=200215121,Param2=6,Param3=85 4.CREATEPROCEDURE s_grade sname char(8),sgrade realoutput AS SELECT sgrade=grade from sc join student on student.sno=sc.sno where sname=sname执行:DECLAREsgrade real EXECs_grade sname=李勇 ,sgrade=sgrade OUTPUT 5.CREATEPROCEDURE s
3、_grade1 ssno char(9),sgrade realoutput AS SELECT sgrade=avg(grade)from sc where sno=ssno 执行:DECLAREsgrade real EXEC s_grade1 ssno=200215121,sgrade=sgrade OUTPUT SELECT sgrade6.CREATE PROCEDURE s_grade2 youbiaocursor varyingoutput AS set=cursor forward_only -forward_only表示从第一条开始往下-static(这里可以添加)forSE
4、LECT sgrade=avg(grade)from sc groupby sno open youbiao -打开游标go执行:名师资料总结-精品资料欢迎下载-名师精心整理-第 1 页,共 12 页 -declare youbiao2 cursor -定义一个游标作参数,用于上面那个存储过程exec guocheng5 youbiao=youbiao2 outputSELECTyoubiao7.if exists(select*from sysobjects where name=guocheng and type=p)-判定存储过程是否存在drop proc guocheng go cre
5、ate proc guocheng id int,name nvarchar(50)as set nocount on set rowcount id -设置要查询多少条数据-select*from adminurl where urlname like name -(1)这里的通配符是在传参数的时候带上的select*from student where urlname like%+name+%-(2)这里的通配符是程序自带的,推荐这个go 8.加密存储过程和实现另外一种模糊查询(用到系统函数)-if exists(select name from sysobjects where name
6、=guocheng2 and type=p)drop proc guocheng2 -判断是否存在go create proc guocheng2 name nvarchar(100)with encryption -实现对存储过程加密,以后谁也看不到内容,所以事先要有备份as set nocount on select*from student where charindex(name,urlname)0 -chaindex的作用相当于 Like name go drop proc guocheng2 exec guocheng2 功能 -存储过程的几种返回值(output,return,s
7、elect)-(1)output存储过程 注意在.NET 中是怎样接受的 if exists(select name from sysobjects where name=guocheng3 and type=p)drop proc guocheng3 -判断是否存在go create proc guocheng3 n int output,-申明是输出参数name nvarchar(50)with encryption -加密as 名师资料总结-精品资料欢迎下载-名师精心整理-第 2 页,共 12 页 -set nocount on -不显示记录数,提高网络select*from stude
8、nt where urlname like%+name+%set n=rowcount -赋值go-开始测试declare n int -定义输出参数exec guocheng3 n output,管理 print n -验证是否输出参数已经赋值-(2)return存储过程,切记 return返回的必须是整型值 注意在.NET 中是怎样接受的 if exists(select name from sysobjects where name=guocheng4 and type=p)drop proc guocheng4 go create proc guocheng4 name nvarcha
9、r(50),n int with encryption as set nocount on set rowcount n select*from student where urlname like%+name+%if(rowcount0)return 1 else return 0 go-开始测试declare m int exec m=guocheng4 管理,4 print m-(3):带返回游标的存储过程,并且游标只能是output类型-【1.定义】if exists(select*from sysobjects where name=guocheng5 and type=p)drop
10、 proc guocheng5 -判断存在否go create proc guocheng5 youbiao cursor varying output -定义一个游标输出参数,varying表示可以变化的as set youbiao=cursor forward_only -forward_only表示从第一条开始往下-static(这里可以添加)for select comment from adminurl -static表示建立一个临时副本,不允许修改基表,如果没有就可以修改基表open youbiao -打开游标go-【2.使用】if exists(select name from
11、sysobjects where name=guocheng6 and type=p)名师资料总结-精品资料欢迎下载-名师精心整理-第 3 页,共 12 页 -drop proc guocheng6 go create proc guocheng6 -用来调用 guocheng5 as declare n nvarchar(100)-定义一个变量用于接收游标的移动的每条记录declare youbiao2 cursor -定义一个游标作参数,用于上面那个存储过程exec guocheng5 youbiao=youbiao2 output -赋值给定义个游标fetch next from you
12、biao2 into n -每条记录赋值while(fetch_status=0)begin if(n=2m)update adminurl set comment=comment+M where current of youbiao2 -能进行修改的前提是上面定义的游标没有static else update adminurl set comment=comment+O where current of youbiao2 -能进行修改的前提是上面定义的游标没有static fetch next from youbiao2 into n -循环赋值end close youbiao2 deal
13、locate youbiao2 go exec guocheng6 -开始执行存储过程6-创建一个带默认值的带判断的存储过程if exists(select name from sysobjects where name=guocheng7 and xtype=p)drop proc guocheng7 go create proc guocheng7 name nvarchar(100)=null,-定义一个默认值是空的输入参数n int output -定义一个输出参数as if name is null -判断参数是否为空begin print error!return end sele
14、ct n=count(*)from student where urlname like%+name+%-给输出参数赋值print n go declare m int -定义临时变量exec guocheng7 管理,m -执行exec guocheng7 n=m -执行带默认值的,但是不能写成exec guocheng7 m-执行远程存储过程-创建连接服务器名师资料总结-精品资料欢迎下载-名师精心整理-第 4 页,共 12 页 -exec sp_addlinkedserver srv_lnk,sqloledb,210.38.202.16 exec sp_addlinkedsrvlogin
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年存储过程、触发器的例子 2022 存储 过程 触发器 例子
限制150内