数据库原理与应用第三版何玉洁第十二章部分上机练习答案(不含游标)
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 [email protected] 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 [email protected]
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 [email protected] 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 [email protected]
return
end
select * from F3_2(‘李勇’)

相关文章:

  • 2021-05-21
  • 2021-11-12
  • 2021-10-09
  • 2021-08-07
  • 2021-05-25
  • 2021-11-23
  • 2021-11-12
  • 2021-04-01
猜你喜欢
  • 2021-08-06
  • 2021-05-20
  • 2021-04-07
  • 2021-07-28
  • 2021-07-07
  • 2021-06-27
  • 2021-04-30
相关资源
相似解决方案