sql serve创建存储过程,查询指定学生的学号、姓名、课程名、成绩
学生表Student(sno,sname,ssex,sage)课程表Lesoon(lno,lname,lcredit)选课表SC(sno,cno,grade)这是我开始写...
学生表Student(sno,sname,ssex,sage)
课程表Lesoon(lno,lname,lcredit)
选课表SC(sno,cno,grade)
这是我开始写的:
if (exists (select * from sys.objects where name = 'proc_stu'))
drop proc proc_stu
go
create proc proc_stu(@sname varchar(8) ='张%')
as
select Student.sno,sname,lname,grade
from Student,SC,Lesson
where Student.sno=SC.sno and SC.lno=Lesson.lno and
sname=@sname
go
由于存在有学生没有选课,所以上面的代码就有问题,参数为没有选课的学生的姓名时,查询结果中就什么都没有。
理想的结果应该是:无论该学生选没选课,结果至少要能有学号和姓名显示,课程名和成绩有就显示,没有就为空 展开
课程表Lesoon(lno,lname,lcredit)
选课表SC(sno,cno,grade)
这是我开始写的:
if (exists (select * from sys.objects where name = 'proc_stu'))
drop proc proc_stu
go
create proc proc_stu(@sname varchar(8) ='张%')
as
select Student.sno,sname,lname,grade
from Student,SC,Lesson
where Student.sno=SC.sno and SC.lno=Lesson.lno and
sname=@sname
go
由于存在有学生没有选课,所以上面的代码就有问题,参数为没有选课的学生的姓名时,查询结果中就什么都没有。
理想的结果应该是:无论该学生选没选课,结果至少要能有学号和姓名显示,课程名和成绩有就显示,没有就为空 展开
2个回答
展开全部
if (exists (select * from sys.objects where name = 'proc_stu'))
drop proc proc_stu
go
create proc proc_stu(@sname varchar(8) ='张%')
as
select Student.sno,sname,isnull(lname,'') as lname,isnull(grade,0) as grade
from Student left join SC on Student.sno=SC.sno
left join Lesson on SC.lno=Lesson.lno and
where SC.sname=@sname
go
drop proc proc_stu
go
create proc proc_stu(@sname varchar(8) ='张%')
as
select Student.sno,sname,isnull(lname,'') as lname,isnull(grade,0) as grade
from Student left join SC on Student.sno=SC.sno
left join Lesson on SC.lno=Lesson.lno and
where SC.sname=@sname
go
展开全部
if (exists (select * from sys.objects where name = 'proc_stu'))
drop proc proc_stu
go
create proc proc_stu(@sname varchar(8) ='张%')
as
select Student.sno,sname,lname,grade
from Student left join SC on Student.sno=SC.sno
left join Lesson on SC.lno=Lesson.lno
where sname like @sname
go
drop proc proc_stu
go
create proc proc_stu(@sname varchar(8) ='张%')
as
select Student.sno,sname,lname,grade
from Student left join SC on Student.sno=SC.sno
left join Lesson on SC.lno=Lesson.lno
where sname like @sname
go
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询