---
title: "PL/SQL编程（高级特性）"
author: "melet"
date: "2007-09-01T08:59:22Z"
tags: []
categories: ["数据库"]
slug: "pl-sql"
draft: false
lastmod: "2007-09-01T08:59:22Z"
id: 119159
url: "https://melet.iteye.com/blog/119159"
---

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

sql 代码

001. /\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**
002. Author:qinyangzhao
003. describe:PL/SQL记录记录 (type is record)
004. \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/
005. set serveroutput on--打开显示模式
006. declare
007. type cust\_record\_type is record(--定义记录类型
008. name customer.name%type,--声明标量变量
009. total ord.total%type--声明记录变量
010. );
011. cust\_record cust\_record\_type;
012. begin
013. select a.name ,b.total ,into cust\_record
014. from customer a ,ord b
015. where a.customer\_id=b.customer\_id and b.ord\_id=&id;
016. dbms\_output.put\_line('客户名'||cust\_record.name);
017. dbms\_output.put\_line('订单总额:'||cust\_record.total);
018. end;
019. /\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**
020. Author:qinyangzhao
021. describe:%rowtype属性(rowtype)
022. \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/
023. delcare
024. product\_record product%rowtype;
025. begin
026. product\_record.product\_id:=&id;
027. product\_record.description:='&description';
028. insert into product values product\_record;
029. end;
030. /\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**
031. Author:qinyangzhao
032. describe:索引表(table)
033. \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/
034. declare
035. type item\_table\_type is table of item.ename%type
036. index by pls\_integer;
037. item\_table item\_table\_type;
038. begin
039. select * bulk collect into item\_table(-1)
040. from item where ord\_id=&id;
041. dbms\_output.put\_line('条款编号:'||item\_table(-1));
042. end;
043. /\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**
044. Author:qinyangzhao
045. describe:嵌套表(table)
046. \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/
047. declare
048. type item\_table\_type is table of item.ename%type;
049. item\_table item\_table\_type;
050. begin
051. item\_table:=item\_table\_type('mary','mary','mary');
052. select ename into item\_table(2) from item
053. where empno=&no;
054. dbms\_output.put\_line('雇员名:'||item\_table(2));
055. end;
056. /\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**
057. Author:qinyangzhao
058. describe:变长数组(array)
059. \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/
060. declare
061. type name\_array\_type is varray(20) of varchar2(30);
062. type city\_array\_type is varray(20) of varchar2(30);
063. name\_array name\_array\_type;
064. city\_array city\_array\_type;
065. begin
066. select name ,city bulk collect
067. into name\_array,city\_array from customer;
068. for i in 1..name\_array.count loop
069. dbms\_output.put\_line('客户名:'||name\_array(i)||',所在城市:'||city\_array(i));
070. end loop;
071. end;
072. /\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**
073. Author:qinyangzhao
074. describe:记录表(table)
075. \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/
076. declare
077. type item\_table\_type is table of item%rowtype
078. index by pls\_integer;
079. item\_table item\_table\_type;
080. begin
081. select * bulk collect into item\_table
082. from item where ord\_id=&id;
083. for i in 1..item\_table.count loop
084. dbms\_output.put\_line('条款编号:'||item\_table(i).item\_id||',总价:'||
085. item\_table(i).total);
086. end loop;
087. end;
088. /\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**
089. Author:qinyangzhao
090. describe:多级(varray)
091. \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/
092. declare
093. type al\_varray\_type is varray(10) of int;--定义一维varray
094. type nal\_varray\_type is varray(10) of al\_varray\_type;--定义二维varrary集合
095. --初始化二维集合变量
096. nvl nal\_varrary\_type:=nal\_varrary\_type(
097. al\_varray\_type(58,100,102),
098. al\_varray\_type(55,6,73),
099. 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;