数据库原理与应用第三版何玉洁第十二章部分上机练习答案(不含游标)

在这里插入图片描述
1-1
create function F1_1(@SSno char(10))
returns int
AS
begin
declare @SumCredit int
select @SumCredit = sum(Credit) from
SC join Course on SC.Cno=Course.Cno
where Sno=@SSno and Grade>=60
return @SumCredit
end

select Cname,dbo.F1_1(Sno) AS 总学分,Credit from
SC join Course on SC.Cno = Course.Cno
where Sno=‘9512101’
1-2
create function F1_2(@Sdept char(10))
returns int
AS
begin
declare @Avggrade tinyint
select @Avggrade = avg(Grade) from SC join Student on SC.Sno = Student.Sno where Sdept=@Sdept
return @Avggrade
end

1-3
create function F1_3(@Sdept char(10),@num int)
returns int
AS
begin
declare @Snum tinyint
select @Snum=count() from Student join SC on Student.Sno = SC.Sno
where Sdept=@Sdept and Ssex = ‘男’
group by Student.Sno
having count(
) >= @num
return @Snum
end
2
2-1
create function F2_1(@low int,@high int)
returns table
as
return
(select Sname,Sdept,Cname from
Student join SC on Student.Sno = SC.Sno join Course on SC.Cno = Course.Cno
where Student.Sno in (select Sno from SC group by Sno having Count(*) between @low and @high)
)
2-2
create function F2_2(@Sdept char(10))
returns table
as
return(
select Sname,Sdept,Cname,Grade from
Student join SC on Student.Sno = SC.Sno join Course on SC.Cno = Course.Cno
where Sdept = @Sdept
)
select Sname,Cname,Grade from F2_2(‘计算机系’)
3
3-1
create function F3_1(@Sdept char(10))
returns @return table(
Sname char(10),
Sage int
)
AS
begin
insert into @return
select top 2 with ties Sname,Sage from Student
where Sdept = @Sdept
order by Sage desc
return
end

3-2
create function F3_2(@name char(10))
returns @return2 table(
Sname char(10),
Sdept char(10),
Cname char(10),
Grade char(10)
)
AS
begin
insert into @return2
select Sname,Sdept,Cname,
case
when Grade >= 90 then ‘优秀’
when Grade between 80 and 89 then ‘良好’
when Grade between 70 and 79 then ‘一般’
when Grade between 60 and 69 then ‘不太好’
when Grade < 60 then ‘很糟糕’
end
from Student join SC on Student.Sno = SC.Sno join Course on SC.Cno=Course.Cno
where Sname=@name
return
end
select * from F3_2(‘李勇’)

猜你喜欢

转载自blog.csdn.net/little_yuan20/article/details/108265553