ʵÑé6 PLSQL³ÌÐòÉè¼Æ

ʵÑé6 PL/SQL³ÌÐòÉè¼Æ

1 ʵÑéÄ¿µÄ

(1) ÕÆÎÕPL/SQL³ÌÐò¿ª·¢·½·¨¡£

(2) ÕÆÎÕ´æ´¢¹ý³Ì¡¢º¯Êý¡¢´¥·¢Æ÷¡¢°üµÄ´´½¨ÓÚµ÷Óá£

2 ʵÑéÒªÇó

(1) ¸ù¾ÝͼÊéÏúÊÛϵͳҵÎñÒªÇó´´½¨Ìض¨µÄ´æ´¢¹ý³Ì¡¢º¯Êý¡¢´¥·¢Æ÷¡£

(2) ¸ù¾ÝͼÊéÏúÊÛϵͳҵÎñÒªÇó½«Í¼ÊéÏúÊÛϵͳÏà¹ØµÄº¯Êý¡¢´æ´¢¹ý³Ì·â×°µ½°üÀï¡£

3 ʵÑé²½Öè

ÒÔbsÓû§µÇ¼BOOKSALESÊý¾Ý¿â£¬ÀûÓÃPL/SQL³ÌÐò±àдÏÂÁй¦ÄÜÄ£¿é¡£

(1) ´´½¨Ò»¸ö´æ´¢¹ý³Ì£¬Êä³ö²»Í¬ÀàÐÍͼÊéµÄÊýÁ¿¡¢Æ½¾ù¼Û¸ñ¡£

SQL> create or replace procedure proc_category_static 2 as

3 --¶¨ÒåÓα꣬»ñÈ¡µ±Ç°ÓÐÄÄЩͼÊéÖÖÀà

4 cursor c_all_category is select distinct category from books; 5 --ͼÊéµÄƽ¾ù¼Û¸ñ 6 v_avg_cost number; 7 begin

8 --±£´æÍ¼ÊéÖÖÀà

9 for v_each_category in c_all_category LOOP

10 select avg(retail) into v_avg_cost category=v_each_category.category group by category;

from

books

where

11 dbms_output.put_line('ÖÖÀàΪ£º'||v_each_category.category||',ƽ¾ù¼Û¸ñΪ£º'|| v_avg_cost);

12 END LOOP;

13 end proc_category_static; 14 /

(2) ´´½¨Ò»¸ö´æ´¢¹ý³Ì£¬ÒÔ¿Í»§ºÅΪ²ÎÊý£¬Êä³ö¸Ã¿Í»§¶©¹ºµÄËùÓÐͼÊéµÄÃû³ÆÓëÊýÁ¿¡£ create or replace procedure proc_get_orderinfo(

2 p_customer_id customers.customer_id%type) 3 as

4 --ÉùÃ÷ÓÎ±ê´æ´¢¿Í»§µÄ¶©µ¥ºÅ

5 cursor c_orderid is select order_id from orders where customer_id=p_customer_id; 6 v_orderid orders.order_id%type; 7 --ÉùÃ÷ÓÎ±ê´æ´¢¶©µ¥ÐÅÏ¢

8 cursor c_orderitem is select ISBN, sum(quantity) totalnum from orderitem where order_id=v_orderid group by ISBN; 9 --±£´æÍ¼ÊéµÄÊéÃû

10 v_title books.title%type; 11

12 begin

13 open c_orderid; 14 LOOP

15 fetch c_orderid into v_orderid;

16 exit when c_orderid%NOTFOUND; 17 for v_orderitem in c_orderitem LOOP

18 select title into v_title from books where ISBN=v_orderitem.ISBN;

19 DBMS_OUTPUT.PUT_LINE(p_customer_id||'¶©¹º'||v_title||'µÄÊýÁ¿ÊÇ'||v_orderitem.totalnum); 20 end LOOP; 21 end LOOP; 22 close c_orderid; 23 end proc_get_orderinfo; 24 /

exec proc_get_orderinfoo(1001);

(3) ´´½¨Ò»¸ö´æ´¢¹ý³Ì£¬ÒÔ¶©µ¥ºÅΪ²ÎÊý£¬Êä³ö¸Ã¶©µ¥ÖÐËùÓÐͼÊéµÄÃû³Æ¡¢µ¥¼Û¡¢ÊýÁ¿¡£ create or replace procedure proc_get_orderinfoo( p_order_id orderitem.order_id%type) as

--ÉùÃ÷ÓÎ±ê´æ´¢¶©µ¥ºÅµÄISBN

cursor c_ISBN is select ISBN from orderitem where order_id=p_order_id; v_ISBN orderitem.ISBN%type; --ÉùÃ÷ÓÎ±ê´æ´¢¶©µ¥ÐÅÏ¢

cursor c_orderitem is select ISBN,sum(quantity) totalnum from orderitem where ISBN=v_ISBN ;

v_title books.title%type; v_retail books.retail%type; begin

open c_ISBN; LOOP

fetch c_ISBN into v_ISBN;

exit when c_ISBN%NOTFOUND;

for v_orderitem in c_orderitem LOOP

select title,retail into v_title,v_retail from books where ISBN=v_orderitem.ISBN; DBMS_OUTPUT.PUT_LINE(p_order_id||v_title||v_retail||v_orderitem.totalnum); end LOOP; end LOOP; close c_ISBN;

end proc_get_orderinfoo; /

ÁªÏµ¿Í·þ£º779662525#qq.com(#Ìæ»»Îª@)