2013-11-11 Oracle 课堂测试 练习题 例:BULK COLLECT及return table
生活随笔
收集整理的這篇文章主要介紹了
2013-11-11 Oracle 课堂测试 练习题 例:BULK COLLECT及return table
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
--1) 查詢“計算機(jī)”專業(yè)學(xué)生在“2007-12-15”至“2008-1-8”時間段內(nèi)借書的
--學(xué)生編號、學(xué)生名稱、圖書編號、圖書名稱、借出日期;
select s.stuid, s.stuname, b.bid, b.title, bo.t_timefrom borrow bojoin student s on bo.stuid = s.stuidjoin book b on bo.bid = b.bidwhere bo.t_time between to_date('2007-12-15', 'yyyy-mm-dd') andto_date('2008-01-08', 'yyyy-mm-dd') and s.major = '計算機(jī)';--2) 用pl/sql匿名塊實(shí)現(xiàn),查詢所有借過圖書的學(xué)生編號、學(xué)生名稱、專業(yè);(提示:用游標(biāo))
declarecursor c_student isselect * from student;cursor c_borrow isselect * from borrow;
begindbms_output.put_line('學(xué)生編號 ' || ' 學(xué)生姓名 ' || ' 專業(yè)');for v_student in c_student loopfor v_borrow in c_borrow loopif v_student.stuid = v_borrow.stuid thendbms_output.put_line(v_student.stuid || ' ' ||v_student.stuname || ' ' ||v_student.major);exit;end if;end loop;end loop;
end;--3) 查詢借過作者為“安意如”的圖書的學(xué)生姓名、圖書名稱、借出日期、歸還日期;
select s.stuname, b.title, bo.t_time, bo.b_timefrom borrow bojoin student s on bo.stuid = s.stuidjoin book b on bo.bid = b.bidwhere b.author = '安意如';--4) 查詢目前借書但未歸還圖書的學(xué)生名稱及未還圖書數(shù)量;
--(要求: 未還圖書數(shù),用函數(shù)實(shí)現(xiàn),并在sql語句中調(diào)用)
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_borrowlistreturn t_borrowlist_table is v_rs t_borrowlist_table;
beginselect t_borrowlist_object(s.stuname, count(*)) BULK COLLECTINTO v_rsfrom borrow bojoin student s on bo.stuid = s.stuidjoin book b on bo.bid = b.bidwhere bo.b_time is nullgroup by s.stuname;return v_rs;
end;select * from table(f_borrowlist());--5)用一個存儲過程完成還書功能,輸入?yún)?shù)為學(xué)號和書號,把當(dāng)前日期作為還書日期。
create or replace function f_rebook(v_stuid student.stuid%type,v_bid book.bid%type) return varchar2 isPRAGMA AUTONOMOUS_TRANSACTION;v_flag number;v_borrowid borrow.borrowid%type;v_rs varchar2(200);
beginselect count(*)INTO v_flagfrom borrowwhere b_time is nulland stuid = v_stuidand bid = v_bid;if v_flag >= 1 thenselect borrowidINTO v_borrowidfrom borrowwhere b_time is nulland stuid = v_stuidand bid = v_bidand rownum <= 1;v_rs := v_stuid || ' 號同學(xué)還 ' || v_bid || ' 圖書 成功';update borrow set b_time = sysdate where borrowid = v_borrowid;commit;elsev_rs := v_stuid || ' 號同學(xué)還 ' || v_bid || ' 圖書 失敗';end if;return v_rs;
end;select * from borrow;select f_rebook('1001', 'B001') from dual;
總結(jié)
以上是生活随笔為你收集整理的2013-11-11 Oracle 课堂测试 练习题 例:BULK COLLECT及return table的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: asp.net mvc3.0第一个程序h
- 下一篇: Oracle11完全卸载方法