资源描述:
《sql server存储过程详细代码(超赞!)》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、usejxgl--首先判断有没有已经建立up_getallstudents存储过程,有则先删除ifexists(selectnamefromsysobjectswherename='up_getallstudents'andtype='p')dropprocedureup_getallstudents--编写存储过程up_getallstudents,用于获取学生表students的所有记录createprocedureup_getallstudentsasselect*fromstudents--使用execute执行存储过程up_getallstudentsexecup_ge
2、tallstudents--也可写成executeup_getallstudents--编写一个存储过程up_insertstudent,完成学生表students数据的插入--1、不带默认值的参数createprocedureup_insertstudent@sidvarchar(15),@snamevarchar(30),@ssexchar(10),@sbirthdatetime,@sbirthplacevarchar(300)asbegininsertintostudents(stu_id,stu_name,stu_sex,stu_birth,stu_birthplace)
3、values(@sid,@sname,@ssex,@sbirth,@sbirthplace)endexecup_insertstudent'200712110111','肖玉峰','男','1975-02-05','山东省滕州市木石镇'--等同于execup_insertstudent@sname='肖玉峰',@sid='200712110111',@ssex='男',@sbirth='1975-02-05',@sbirthplace='山东省滕州市木石镇'dropprocedureup_insertstudentdeletestudentswherestu_name='肖玉峰'
4、insertintostudents(stu_id,stu_name,stu_sex,stu_birth,stu_birthplace)values('200712110110','马缪','男','1986-010-1700:00:00.000','广东广州')--编写一个存储过程up_delstudentbyname,根据输入的学生姓名,删除该学生记录ifobject_id('up_delstudentbyname','p')isnotnulldropprocedureup_delstudentbynamegocreateprocedureup_delstudentbynam
5、e@snamevarchar(30)asbegindeletefromstudentswherestu_name=@snameend--调用存储过程的代码如下:execup_delstudentbyname'马缪'select*fromstudents--编写一个存储过程up_getstuinformationbyname,根据输入的学生姓名,显示该学生的学号、姓名、课程名和成绩ifobject_id('up_getstuinformationbyname','p')isnotnulldropprocedureup_getstuinformationbynamegocreatep
6、rocedureup_getstuinformationbyname@snamevarchar(30)asbeginselect"S".stu_id,stu_name,cour_name,scorefromstudents"S",courses"C",course_score"CS"where"S".stu_id="CS".stu_idand"CS".cour_id="C".cour_idandstu_name=@snameend--调用存储过程的代码如下:execup_getstuinformationbyname'仇立权'--ok--2、带默认值的参数--编写一个存储过程up
7、_insertstuwithdefault,给参数定义默认值,完成学生表students数据的插入dropprocedureup_insertstuwithdefaultgocreateprocedureup_insertstuwithdefault@sidvarchar(15),@snamevarchar(30),@ssexchar(10)='男',@sbirthdatetime,@sbirthplacevarchar(300)='',@semailvarchar(50)=''