首页 > 数据库 >2013-11-11 Oracle 课堂测试 练习题 例:BULK COLLECT及return table

2013-11-11 Oracle 课堂测试 练习题 例:BULK COLLECT及return table

时间:2022-11-20 20:34:43浏览次数:61  
标签:11 练习题 return bid bo borrow student stuid select


--1)  查询“计算机”专业学生在“2007-12-15”至“2008-1-8”时间段内借书的
--学生编号、学生名称、图书编号、图书名称、借出日期;
select s.stuid, s.stuname, b.bid, b.title, bo.t_time
from borrow bo
join student s on bo.stuid = s.stuid
join book b on bo.bid = b.bid
where bo.t_time between to_date('2007-12-15', 'yyyy-mm-dd') and
to_date('2008-01-08', 'yyyy-mm-dd') and s.major = '计算机';

--2) 用pl/sql匿名块实现,查询所有借过图书的学生编号、学生名称、专业;(提示:用游标)
declare
cursor c_student is
select * from student;
cursor c_borrow is
select * from borrow;
begin
dbms_output.put_line('学生编号 ' || ' 学生姓名 ' || ' 专业');
for v_student in c_student loop
for v_borrow in c_borrow loop
if v_student.stuid = v_borrow.stuid then
dbms_output.put_line(v_student.stuid || ' ' ||
v_student.stuname || ' ' ||
v_student.major);
exit;
end if;
end loop;
end loop;
end;

--3) 查询借过作者为“安意如”的图书的学生姓名、图书名称、借出日期、归还日期;
select s.stuname, b.title, bo.t_time, bo.b_time
from borrow bo
join student s on bo.stuid = s.stuid
join book b on bo.bid = b.bid
where b.author = '安意如';

--4) 查询目前借书但未归还图书的学生名称及未还图书数量;
--(要求: 未还图书数,用函数实现,并在sql语句中调用)
create or replace type t_borrowlist_object as object(stuid varchar2(20),
books number);
create or replace type t_borrowlist_table as table of t_borrowlist_object;

create or replace function f_borrowlist
return t_borrowlist_table is v_rs t_borrowlist_table;
begin

select t_borrowlist_object(s.stuname, count(*)) BULK COLLECT
INTO v_rs
from borrow bo
join student s on bo.stuid = s.stuid
join book b on bo.bid = b.bid
where bo.b_time is null
group by s.stuname;

return v_rs;
end;

select * from table(f_borrowlist());

--5)用一个存储过程完成还书功能,输入参数为学号和书号,把当前日期作为还书日期。
create or replace function f_rebook(v_stuid student.stuid%type,
v_bid book.bid%type) return varchar2 is
PRAGMA AUTONOMOUS_TRANSACTION;
v_flag number;
v_borrowid borrow.borrowid%type;
v_rs varchar2(200);
begin

select count(*)
INTO v_flag
from borrow
where b_time is null
and stuid = v_stuid
and bid = v_bid;

if v_flag >= 1 then
select borrowid
INTO v_borrowid
from borrow
where b_time is null
and stuid = v_stuid
and bid = v_bid
and rownum <= 1;
v_rs := v_stuid || ' 号同学还 ' || v_bid || ' 图书 成功';
update borrow set b_time = sysdate where borrowid = v_borrowid;
commit;
else
v_rs := v_stuid || ' 号同学还 ' || v_bid || ' 图书 失败';
end if;

return v_rs;
end;

select * from borrow;

select f_rebook('1001', 'B001') from dual;





标签:11,练习题,return,bid,bo,borrow,student,stuid,select
From: https://blog.51cto.com/yuzhyn/5871951

相关文章

  • 【2022.11.19】服务器常用命令
    cd/etc/apt/mvsources.listsources.list.baknanosources.listdebhttp://mirrors.aliyun.com/debian/bullseyemainnon-freecontribdebhttp://mirrors.aliyun......
  • Ubuntu 16.04安装Brackets 1.7失败,缺失libgcrypt11,解决办法
    Ubuntu16.04安装Brackets1.7失败,缺失libgcrypt11,解决办法mingdu.zhengatgmaildotcom问题Ubuntu16.04安装Brackets1.7失败,提示缺失libgcrypt11。sudodpkg-iBracke......
  • 2022年11月20收获
    2022年11月20日C语言收获1、电容器通过高频率交流电,容器越大的电容器越容易通过交流电,电容器对直流电起到阻隔作用。2、W;角速度和角频率=2Πf3、整流通过2极管整流将正......
  • debian11安装php8
    1、​​https://www.php.net/downloads​​ 下载对应的版本sudowget​​​​https://www.php.net/distributions/php-8.1.12.tar.bz2​​2、tar-zxvfphp-8.1.12.tar.b......
  • 2022-2023-1 20221311 《计算机基础与程序设计》第十二周学习总结
    班级链接:https://edu.cnblogs.com/campus/besti/2022-2023-1-CFAP作业要求:https://www.cnblogs.com/rocedu/p/9577842.html#WEEK08作业目标:作业目标:《C语言程序设计》第11......
  • 100011 求正方体棱长总和表面积体积已知边长
    <?phpheader('Content-Type:text/html;charset=utf-8');define('ROOT',$_SERVER['DOCUMENT_ROOT']);includeROOT.'/assets/php/head.php';$tit='求正方体棱长总......
  • 11月18学习总结
    11月18学习总结一、软件开发目录1.软件开发框架软件开发框架:在编写项目之前需要遵循的代码层面上面的规范(运行流程,环节,步骤)2.软件开发框架的分类1.c/s框架client/......
  • 11.15-11.18周末小结
    目录一、软件开发架构1.C/S架构2.B/S架构二、网络编程简介1.什么是网络编程2.学习网络编程的目的3.网络编程的发展史4.网络编程必备条件四:OSI七层协议模型和TCP/IP四层协议......
  • L10U6-4-Planning-your-career-path-20221120
    1VocabularyDescribinggoals2ExpressionsPlansandgoals3ReadingCareercounselor4FinalTaskPlanningacareerpath......
  • Windows 10/11 家庭版中使用组策略gpedit.msc
    保存为了gpedit.bat,使用管理员身份运行@echooffpushd"%~dp0"dir/bC:\Windows\servicing\Packages\Microsoft-Windows-GroupPolicy-ClientExtensions-Package~3*.mum......