利用Create Procedure语句创建一个带有参数的存储过程“ProcInsert”,向“student_course”表插入一条记录(‘B0451101’,‘30020108’,86),如果“B0451101”号学生“30020108”号课程记录已经存在,则设置状态值为-101,并输出提示信息“该学生已经在该门课程的成绩已经存在”,否则设置状态值为10,并输出提示信息“该条记录可以添加”。
代码如下:
```create procedure ProcInsert
as
declare @state int=0
select count(*) from student_course where sno='B0451101'
if count(*)>0
begin
set @state=-101
print '该学生已经在该门课程的成绩已经存在'
end
else
begin
set @state=10
print '该条记录可以添加'insert into student_course(sno,tcid,score)
values('B0451101','30020108',86)
select a.sname,b.cname,c.score
from student a left join course b left join student_course c
on b.cno=c.tcid
on a.sno=c.sno
where @state=10
end
go