ITeye博客

PL/SQL编程(高级特性)

melet · 2007-09-01 08:59 · DB · 数据库 · Markdown

下文为plsql编程中高级特性的应用实例。以供大家在编程时参考。

sql 代码

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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。