实验内容:
- 使用IF ELSE查询马行空是否选修了数据库课程,如果有显示“已选修”,没有显示“没选修”;
- 使用IF ELSE BEGIN END查询选修课程003的情况,如果不为空则显示“有学生选修”,并查询选修课程的人数,否则显示“无学生选修”;
- 使用WHILE查询学号为00001、00002、00003、00004,课程号为002的成绩,并分别把成绩赋予给变量@s,然后显示出来;
- 使用WHILE BREAK查询学号为00001、00002、00003、00004,课程号为002的成绩,并分别把成绩赋予给变量@s,如果成绩为47,则显示“成绩过低”,程序结束,否则显示成绩;
- 使用WHILE CONTINUE查询学号为00001、00002、00003、00004,课程号为002的成绩,并分别把成绩赋予给变量x,如果成绩为47,则不显示,否则显示成绩;
- 指示SQL SERVER等待10s后查询表Student;
- 使用CASE表达式查询SC情况,如果CNO=001则显示“数据结构”,如果CNO=002则显示“数据库”,如果CNO=003则显示“C语言”,其它则显示“JAVA”;
- 使用GOTO查询名字为“吕小妹”的同学,如果存在,显示该同学的信息;否则显示“查无此人”;
- 自定义一个函数,实现如下功能:对于一个给定的学号,查询该学号是否在Student表中,如果在则返回0,否则返回-1,然后编程调用该函数测试;
- 自定义一个函数,实现如下功能:对于一个给定的学生姓名,查询该姓名是否在Student表中,如果有两个或两个以上相同的名字,则返回-2,如果只有唯一名字,则返回该学生所修的总学分(及格才算有学分),如果没有该名字,则返回-1,然后编程调用该函数测试;
- 自定义一个函数,实现如下功能:对于一个给定的课程号,查询没有修读该课程的学生名字。
源代码:
/*1.使用IF ELSE BEGIN END查询选修课程003的情况,如果不为空则显示“有学生选修”,并查询选修课程的人数,否则显示“无学生选修”*/
if exists(select *
from student,sc,course
where sname='马行空'and student.sno=sc.sno and cname='数据库')
print '已选修'
else
print'没选修'
/*2.使用IF ELSE BEGIN END查询选修课程003的情况,如果不为空则显示“有学生选修”,并查询选修课程的人数,否则显示“无学生选修”*/
if exists(select * from sc where cno='003')
begin
print '有学生选修'
select count(*)
from sc
where cno='003'
end
else print'无学生选修'
/*3.使用WHILE查询学号为00001、00002、00003、00004,课程号为002的成绩,并分别把成绩赋予给变量@s,然后显示出来;*/
declare @x int,@y varchar(5),@s int
set @x=1
set @y=00001
while @y!='00005'
begin
select @s=score
from sc
where cno='002' and sno=@y
print @s
set @x=@x+1
set @y='0000'
set @y=@y+convert(char(1),@x)
end
/*4.使用WHILE BREAK查询学号为00001、00002、00003、00004,课程号为002的成绩,并分别把成绩赋予给变量@s,如果成绩为47,则显示“成绩过低”,程序结束,否则显示成绩;*/
declare @x1 int,@y1 varchar(5),@s1 int
set @x1=1
set @y1=00001
while @y1!='00005'
begin
select @s1=score
from sc
where cno='002' and sno=@y1
if @s1=47
begin
print'成绩过低'
break
end
else
begin
print @s1
set @x1=@x1+1
set @y1='0000'
set @y1=@y1+convert(char(1),@x1)
end
end
/*5 使用WHILE CONTINUE查询学号为00001、00002、00003、00004,课程号为002的成绩,并分别把成绩赋予给变量x,如果成绩为47,则不显示,否则显示成绩;*/
declare @x2 int,@y2 varchar(5),@s2 int
set @x2=1
set @y2=00001
while @y2!='00005'
begin
select @s2=score
from sc
where cno='002' and sno=@y2
set @x2=@x2+1
set @y2='0000'
set @y2=@y2+convert(char(1),@x2)
if @s2=47
continue
print @s2
end
/*6.指示SQL SERVER等待10s后查询表Student;*/
waitfor delay'00:00:10'
select *
from student
/*7.使用CASE表达式查询SC情况,如果CNO=001则显示“数据结构”,如果CNO=002则显示“数据库”,如果CNO=003则显示“C语言”,其它则显示“JAVA”*/
select SNo,Score,CNo =
case CNo
when '001' then '数据结构'
when '002' then '数据库'
when '003' then 'C语言'
else 'JAVA'
end
from sc
where CNo = '001' or CNo = '002' or CNo = '003' or CNo = '004'
/*8.使用GOTO查询名字为“吕小妹”的同学,如果存在,显示该同学的信息;否则显示“查无此人
*/
if exists(select SNo from Student where SName = '吕小妹')
goto nation
else
begin
print '查无此人'
return
end
nation:
select *
from Student
where SName = '吕小妹'
/*9.自定义一个函数,实现如下功能:对于一个给定的学号,查询该学号是否在Student表中,如果在则返回0,否则返回-1;*/
create function checkSno(@Sno char(8)) returns int
as
begin
declare @n int
if(exists(select * from Student where Sno=@Sno))
set @n = 1
else
begin
set @n = 0
end
return @n
end
select dbo.checkSno('00002')
/*10.自定义一个函数,对于一个给定的学生姓名,查询该姓名是否在Student表中,如果有两个或两个以上相同的名字,则返回-2,如果只有唯一名字,则返回该学生所修的总学分(及格才算有学分),如果没有该名字,则返回-1,然后编程调用该函数测试;*/
create function checkSName(@SName varchar(10)) returns int
as
begin
declare @n int,@num int
if(exists(select * from Student where SName=@SName))
begin
set @num=(select count(SName) from Student where SName=@SName)
if @num>=2
begin
set @n=-2
end
else
begin
set @n=(select sum(CCredits)
from sc,course,student
where sc.cno=course.cno and Score>=60 and Student.sno=sc.sno and SName=@SName )
end
end
else
begin
set @n=-1
end
return @n
end
select dbo.checkSName('马行空')
/*11.自定义一个函数,实现如下功能:对于一个给定的课程号,查询没有修读该课程的学生名字;*/
create function checkCno(@Cno char(5)) returns table
as return(
select SName
from Student
where SNo not in(
select SNo
from sc
where CNo=@Cno))
select *
from checkCno('002')