oracle存储过程_备用

原文:http://blog.csdn.net/caoshichaocaoshichao/article/details/5349576

  1.    describe:PL/SQL记录记录 (type is record)                             
  2.                                                       
  3. *********************************************************/   
  4. set serveroutput on--打开显示模式   
  5. declare  
  6.     type cust_record_type is record(--定义记录类型   
  7.         name customer.name%type,--声明标量变量   
  8.         total ord.total%type--声明记录变量   
  9.     );   
  10.     cust_record cust_record_type;   
  11. begin  
  12.     select a.name ,b.total ,into cust_record   
  13.     from customer a ,ord b   
  14.     where a.customer_id=b.customer_id and b.ord_id=&id;   
  15.     dbms_output.put_line('客户名'||cust_record.name);   
  16.     dbms_output.put_line('订单总额:'||cust_record.total);   
  17. end;     
  18. /*********************************************************   
  19.                                                       
  20.    Author:qinyangzhao                                  
  21.    describe:%rowtype属性(rowtype)                              
  22.                                                        
  23. *********************************************************/   
  24. delcare   
  25.     product_record product%rowtype;   
  26. begin  
  27.     product_record.product_id:=&id;   
  28.     product_record.description:='&description';   
  29.     insert into product values product_record;   
  30. end;     
  31. /*********************************************************   
  32.                                                        
  33.    Author:qinyangzhao                                  
  34.    describe:索引表(table)                               
  35.                                                        
  36. *********************************************************/       
  37. declare  
  38.     type item_table_type is table of item.ename%type   
  39.     index by pls_integer;   
  40.     item_table item_table_type;   
  41. begin  
  42.     select * bulk collect into item_table(-1)   
  43.     from item where ord_id=&id;      
  44.     dbms_output.put_line('条款编号:'||item_table(-1));   
  45.        
  46. end;           
  47.   
  48. /*********************************************************   
  49.                                                        
  50.    Author:qinyangzhao                                  
  51.    describe:嵌套表(table)                               
  52.                                                        
  53. *********************************************************/       
  54. declare  
  55.     type item_table_type is table of item.ename%type;      
  56.     item_table item_table_type;   
  57. begin  
  58.     item_table:=item_table_type('mary','mary','mary');   
  59.     select ename into item_table(2) from item   
  60.     where empno=&no;   
  61.     dbms_output.put_line('雇员名:'||item_table(2));   
  62. end;     
  63. /*********************************************************   
  64.                                                       
  65.    Author:qinyangzhao                                  
  66.    describe:变长数组(array)                                
  67.                                                       
  68. *********************************************************/   
  69. declare  
  70.   
  71.     type name_array_type is varray(20) of varchar2(30);   
  72.     type city_array_type is varray(20) of varchar2(30);   
  73.     name_array name_array_type;   
  74.     city_array city_array_type;   
  75. begin           
  76.     select name ,city bulk collect   
  77.         into name_array,city_array from customer;   
  78.     for i in 1..name_array.count loop   
  79.         dbms_output.put_line('客户名:'||name_array(i)||',所在城市:'||city_array(i));   
  80.     end loop;       
  81. end;   
  82. /*********************************************************   
  83.                                                        
  84.    Author:qinyangzhao                                  
  85.    describe:记录表(table)                               
  86.                                                        
  87. *********************************************************/       
  88. declare  
  89.     type item_table_type is table of item%rowtype   
  90.     index by pls_integer;   
  91.     item_table item_table_type;   
  92. begin  
  93.     select * bulk collect into item_table   
  94.     from item where ord_id=&id;   
  95.     for i in 1..item_table.count loop   
  96.         dbms_output.put_line('条款编号:'||item_table(i).item_id||',总价:'||   
  97.             item_table(i).total);   
  98.      end loop;   
  99. end;        
  100. /*********************************************************   
  101.                                                        
  102.    Author:qinyangzhao                                  
  103.    describe:多级(varray)                               
  104.                                                        
  105. *********************************************************/     
  106. declare  
  107.     type al_varray_type is varray(10) of int;--定义一维varray   
  108.     type nal_varray_type is varray(10) of al_varray_type;--定义二维varrary集合   
  109.     --初始化二维集合变量   
  110.     nvl nal_varrary_type:=nal_varrary_type(   
  111.         al_varray_type(58,100,102),   
  112.         al_varray_type(55,6,73),   
  113.         al_arrary_type(2,4));   
  114. begin  
  115.     dbms_output.put_line('显示二维数组所有元素');   
  116.     for i in 1..nvl.count loop   
  117.         for j in 1..nvl(i).count loop   
  118.             dbms_output.put_line('nvl('||i||','||j||')='||nvl(i,j));   
  119.         end loop;   
  120.     end loop;   
  121. end;                       
  122. /*********************************************************   
  123.                                                        
  124.    Author:qinyangzhao                                  
  125.    describe:多级(嵌套)                               
  126.                                                        
  127. *********************************************************/     
  128. declare  
  129.     type al_table_type is table of int;--定义一维嵌套表   
  130.     type nal_table_type is table of al_table_type;--定义二维嵌套表集合   
  131.     --初始化二维集合变量   
  132.     nvl nal_varrary_type:=nal_varrary_type(   
  133.         al_varray_type(58,100,102),   
  134.         al_varray_type(55,6,73),   
  135.         al_arrary_type(2,4));   
  136. begin  
  137.     dbms_output.put_line('显示二维数组所有元素');   
  138.     for i in 1..nvl.count loop   
  139.         for j in 1..nvl(i).count loop   
  140.             dbms_output.put_line('nvl('||i||','||j||')='||nvl(i,j));   
  141.         end loop;   
  142.     end loop;   
  143. end;     
  144. /*********************************************************   
  145.                                                        
  146.    Author:qinyangzhao                                  
  147.    describe:多级(索引表)                               
  148.                                                        
  149. *********************************************************/     
  150. declare  
  151.     type al_table_type is table of int  
  152.     index by binary_integer;--定义一维table   
  153.     type nal_table_type is table of al_table_type   
  154.     index by binary_integer;--定义二维table集合       
  155.     nvl nal_varrary_type;   
  156. begin  
  157.     --初始化二维集合变量   
  158.     nvl(1)(1):=10;   
  159.     nvl(1)(2):=5;   
  160.     nvl(2)(1):=32;   
  161.     nvl(2)(2):=88;   
  162.     dbms_output.put_line('显示二维数组所有元素');   
  163.     for i in 1..nvl.count loop   
  164.         for j in 1..nvl(i).count loop   
  165.             dbms_output.put_line('nvl('||i||','||j||')='||nvl(i,j));   
  166.         end loop;   
  167.     end loop;   
  168. end;                 
  169. /*********************************************************   
  170.                                                        
  171.    Author:qinyangzhao                                  
  172.    describe:处理多行查询语句                              
  173.                                                        
  174. *********************************************************/                     
  175. declare  
  176.    type empcurtyp is ref cursor;   
  177.    emp_cv empcurtyp;   
  178.    emp_record emp%rowtype;   
  179.    sql_stat varchar2(100);   
  180. begin  
  181.    sql_stat:='select * from emp where deptno:=dno';   
  182.    open emp_cv for sql_stat using &dno;   
  183.    loop   
  184.        fetch emp_cv into emp_record;   
  185.        exit when emp_cv%notfound ;   
  186.        dbms_output.put_line('雇员名:'||emp_record.ename||',工资:'||emp_record.sal);   
  187.    end loop;   
  188.    close emp_cv;   
  189. end;   
  190. /*********************************************************   
  191.                                                        
  192.    Author:qinyangzhao                                  
  193.    describe:使用bulk子句处理dml语句返回子句                             
  194.                                                        
  195. *********************************************************/       
  196. declare      
  197.     type ename_table_type is table of emp.ename%type   
  198.       index by binary_integer;   
  199.     type sal_table_type is table of emp.sal%type   
  200.       index by binary_integer;   
  201.       ename_table ename_table_type;   
  202.       sal_table sal_table_type;   
  203.       sql_stat varchar2(100);   
  204. begin  
  205.     sql_stat:='update emp set sal=sal*(1+:percent/100)'   
  206.            ||'where deptno=:dno'   
  207.            ||'returning ename,sal into :name,:salary';   
  208.     execute immediate sql_stat using &percen ,&dno returning bulk collect into ename_table,sal_table;   
  209.     for i in 1..ename_table.count loop   
  210.        dbms_output.put_line('雇员:'||ename_table(i)   
  211.              ||',的新工资为'|| sal_table(i));   
  212.     end loop;                            
  213. end;     
  214. /*********************************************************   
  215.                                                        
  216.    Author:qinyangzhao                                  
  217.    describe:使用bulk子句处理多行查询                             
  218.                                                        
  219. *********************************************************/         
  220. declare    
  221.     type ename_table_type is table of emp.ename%type   
  222.        index by binary_integer;   
  223.     ename_table ename_table_type;   
  224.     sql_stat varchar2(100);   
  225. begin  
  226.     sql_stat:='select ename from emp where deptno+:dno';   
  227.     execute immediate sql_stat bulk collect into ename_table using &dno;   
  228.     for i in 1..ename_table.count loop   
  229.         dbms_output.put_line(ename_table(i));   
  230.     end loop;       
  231. end;   
  232. /*********************************************************   
  233.                                                        
  234.    Author:qinyangzhao                                  
  235.    describe:在fetch语句中使用bulk子句                           
  236.                                                        
  237. *********************************************************/       
  238. declare     
  239.     type empcurtyp is ref cursor;   
  240.     emp_cv empcurtyp ;   
  241.     type ename_table_type is table of emp.ename%type   
  242.        index by binary_integer;   
  243.     ename_table ename_table_type;   
  244.     sql_stat varchar2(100);   
  245. begin  
  246.     sql_stat:='select ename from emp where job:title';   
  247.     open emp_cv for sql_stat using '&job';   
  248.     fetch emp_cv bulk collect into ename_table;   
  249.     for i in 1..ename_table.count loop   
  250.       dbms_output.put_line(ename_table(i));   
  251.     end loop;   
  252.     close emp_cv;   
  253. end;               
  254.  /*********************************************************   
  255.                                                        
  256.    Author:qinyangzhao                                  
  257.    describe:在forall语句中使用bulk子句                           
  258.                                                        
  259. *********************************************************/        
  260. declare  
  261.     type ename_table_type is table of emp.ename%type;   
  262.     type sal_table_type is table of emp.sal%type;   
  263.     ename_table ename_table_type;   
  264.     sal_table sal_table_type;   
  265.     sql_stat varchar2(100);   
  266. begin  
  267.     ename_table:=name_table_type('scott','smith','clark');   
  268.     sql_stat:='update emp set sal=sal*1.1 where ename=:1'   
  269.            ||'returning sal into :2';   
  270.     forall i in 1..ename_talbe.count  
  271.         execute immediate sql_stat using ename_table(i)   
  272.             returing bulk collect into sal_table ;   
  273.     for j in 1..ename_table.count loop   
  274.         dbms_output.put_line('雇员'||ename_table(j)          
  275.             ||'的新工资为'||sal_table(j));   
  276.     end loop;   
  277. end;   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值