储存过程的创建与调用
不带参数的SQLServer 储存过程的创建和调用
SET NOCOUNT ON
GO
USE JX
GO
--********** 1、创建存储过程pro_SelectStuAll,用于查询所有学生信息**********--
--********** Begin **********--
create procedure pro_SelectStuAll
as
begin
select * from Student
end
--********** End **********--
GO
--********** 2、创建存储过程pro_DeleteNoSC,用于删除所有没有选修课程的同学信息**********--
--********** Begin **********--
create procedure pro_DeleteNoSC
as
begin
delete from Student
where not exists (
select 1
from sc
where sc.sno=Student.sno
)
end
--********** End **********--
GO
--***********3、创建存储过程pro_UpdateSdept,用于把'计算机'系的学生系别修改为'计算机工程'***********--
--********** Begin **********--
create procedure pro_UpdateSdept
as
begin
update Student set sdept='计算机工程' where sdept='计算机'
end
--********** end **********--
GO
--4、调用存储过程pro_SelectStuAll--
--********** Begin **********--
exec pro_SelectStuAll
--********** end **********--
GO
--5、调用存储过程pro_DeleteNoSC--
--********** Begin **********--
exec pro_DeleteNoSC
--********** end **********--
GO
--6、调用存储过程pro_UpdateSdept--
--********** Begin **********--
exec pro_UpdateSdept
--********** end **********--
-----------------------------------
GO
SELECT * FROM Student
带输入参数的存储过程的创建和调用、
SET NOCOUNT ON
GO
USE JX
GO
--**********1、创建存储过程pro_SelecByJSJ,用以删除指定Sdept的学生信息*************--
--********** Begin **********--
create procedure pro_SelecByJSJ
@Sdept varchar(20)
as
Begin
delete from Student where Sdept=@sdept
end
--********** end **********--
GO
--**********2、创建存储过程pro_SelecByTprof,用以按照用户指定的职称查询教师的基本信息 *************--
--********** Begin **********--
create procedure pro_SelecByTprof
@tprof varchar (10),
@tsex char(2)
as
Begin
SELECT * from Teacher where tprof=@tprof and tsex=@tsex
end
--********** end **********--
GO
--**********3、创建存储过程pro_AddTeacher,用以插入指定教工号和姓名的教师信息 *************--
--********** Begin **********--
create procedure pro_AddTeacher
@tno char(5),
@tname varchar(12)
as
begin
insert into Teacher(tno,tname ) values(@tno,@tname)
end
--********** end **********--
GO
--**********4、调用存储过程pro_SelecByJSJ,用于删除'计算机'系学生的信息 *************--
--********** Begin **********--
exec pro_SelecByJSJ '计算机'
--********** end **********--
GO
--**********5、调用存储过程pro_SelecByTprof,用于查询所有职称为'副教授'的女教师信息 *************--
--********** Begin **********--
exec pro_SelecByTprof '副教授','女'
--********** end **********--
GO
--**********6、调用存储过程pro_AddTeacher,用于插入两名教师信息,他们的教工号和姓名分别是:'005','测试君1'和'006','测试君2' *************--
--********** Begin **********--
exec pro_AddTeacher '005','测试君1'
exec pro_AddTeacher '006','测试君2'
--********** end **********--
GO
SELECT * FROM Student
SELECT * FROM Teacher
带返回值的存储过程的创建和调用
SET NOCOUNT ON
GO
USE JX
GO
--**********1. 创建一个存储过程Pro_com,用于查询指定的学号是否存在选修课的情况,若存在,则输出该生的学号、姓名及选课门数信息并返回1,否则返回0 *************--
--********** Begin **********--
create procedure Pro_com
@sno char (5),
@res int output
as
begin
declare @cnt int
select @cnt=count(*)
from sc
where sc.sno=@sno
if @cnt>0
begin
select s.sno 学号,s.sname 姓名,@cnt 选课门数
from student s
where s.sno=@sno
set @res=1
end
else
begin
set @res=0
end
end
--********** end **********--
GO
--**********2. 用存储过程Pro_com,用于查询学号为“21001”的同学的选课情况,若返回值为0的,打印输出“不存在该同学的选课信息!”*************--
--********** Begin **********--
declare @res int
exec Pro_com '21001' ,@res output
if @res=0
begin
print "不存在该同学的选课信息"
end
--********** end **********--
GO
--**********3. 用存储过程Pro_com,用于查询学号为“21011”的同学的选课情况,若返回值为0的,打印输出“不存在该同学的选课信息!”*************--
--********** Begin **********--
declare @res int
exec Pro_com '21011' ,@res output
if @res=0
begin
print "不存在该同学的选课信息!"
end
--********** end **********--
GO
posted on 2025-06-18 19:33 swj2529411658 阅读(15) 评论(0) 收藏 举报
浙公网安备 33010602011771号