1. 为公司一个项目没有接触过oracle的程序员准备的一个oracle如何使用proc实现增删改查,简单示例: 
  2.  
  3.  
  4. create table t1 
  5. sid number not null primary key
  6. sname varchar2(10) 
  7. tablespace test; 
  8.  
  9.  
  10. declare 
  11. a number :=1; 
  12. begin 
  13. loop  
  14. insert into t1 values(a,'snow'); 
  15. a:=a+1; 
  16. exit when a=100; 
  17. end loop; 
  18. end
  19.  
  20.  
  21. ----1.insert 
  22.  
  23.  
  24. create or replace procedure proc_insert 
  25. sid number, 
  26. sname varchar2 
  27. is  
  28. begin 
  29.   insert into scott.t1(sid,sname) values(sid,sname); 
  30.    dbms_output.put_line(' 影响的行数:   '||sql%rowcount); 
  31.   commit
  32. end 
  33.  
  34.  
  35. set serveroutput on 
  36. exec proc_insert(101,'snow'); 
  37.  
  38. ----2.update 
  39.  
  40. create or replace procedure proc_update 
  41. isid in number , 
  42. nsname in varchar2  
  43. is  
  44. begin 
  45.   update scott.t1 set sname=nsname where sid=isid; 
  46. If  SQL%Found  Then 
  47.     DBMS_OUTPUT.PUT_LINE('更新成功!'); 
  48. Else 
  49.     DBMS_OUTPUT.PUT_LINE('更新失败!'); 
  50. End  If; 
  51.   commit
  52. end 
  53.  
  54.  
  55. set serveroutput on 
  56. exec proc_update(101,'ocpyang'); 
  57.  
  58.  
  59. ----3.delete 
  60.  
  61.  
  62. create or replace procedure proc_delete 
  63. isid in number  
  64. is  
  65. begin 
  66.   delete scott.t1  where sid=isid; 
  67. If  SQL%Found  Then 
  68.     DBMS_OUTPUT.PUT_LINE('删除成功!'); 
  69. Else 
  70.     DBMS_OUTPUT.PUT_LINE('删除失败!'); 
  71. End  If; 
  72.   commit
  73. end 
  74.  
  75.  
  76. set serveroutput on 
  77. exec proc_delete(101); 
  78.  
  79. --------------4.select 
  80.  
  81. --4.1变量(select ....into):单行查询操作 
  82.  
  83.  
  84. create or replace procedure proc_select0 
  85. (isid in t1.sid%type )  --输入参数 
  86. as 
  87. osid t1.sid%type;  --变量  
  88. osname  t1.sname%type;   --变量  
  89. begin 
  90. select sid,sname into osid, osname from t1 where sid=isid; 
  91. dbms_output.put_line(' 编号为'||osid|| ' , 的职工姓名为  '||osname ); 
  92. exception 
  93. when no_data_found then 
  94. dbms_output.put_line('没有符合条件的记录!'); 
  95. when too_many_rows then 
  96. dbms_output.put_line('返回的行数太多!'); 
  97. when others then 
  98. dbms_output.put_line('发生意外错误!'); 
  99. end
  100.  
  101.  
  102. set serveroutput on 
  103. exec proc_select0 (101); 
  104.  
  105.  
  106. ---4.2显示游标:返单行单列记录  
  107.  
  108. create or replace procedure proc_select1 
  109. (isid in t1.sid%type )  --输入参数 
  110. as 
  111. cursor a is select sname from t1 where sid=isid; 
  112. osname t1.sname%type; 
  113. begin 
  114. open a; 
  115. fetch a into osname;  
  116. if a%found then 
  117. dbms_output.put_line( '职工姓名为:'||osname );  --游标结果集中只有一列 
  118. else 
  119. dbms_output.put_line('没有符合条件的记录!'); 
  120. end if; 
  121. close a; 
  122. end
  123.          
  124. set serveroutput on 
  125. exec proc_select1 (101); 
  126.  
  127. --4.3显示游标:返回单行多列记录 
  128. create or replace procedure proc_select2 
  129. (isid in t1.sid%type )  --输入参数 
  130. as 
  131. cursor a is select * from t1 where sid=isid ; 
  132. osname t1%rowtype; 
  133. begin 
  134. open a; 
  135. fetch a into osname;  
  136. if a%found then  
  137. dbms_output.put_line( '职工的编号为:'||osname.sid||';'||'的职工姓名为  '||osname.sname ); 
  138. else 
  139. dbms_output.put_line('没有符合条件的记录!'); 
  140. end if; 
  141. close a; 
  142. end
  143.    
  144.        
  145. set serveroutput on 
  146. exec proc_select2 (101); 
  147.  
  148. ---4.4显示游标(loop循环):返回多行多列记录 
  149.  
  150. /* 
  151.  
  152. exit when语句一定要紧跟在fetch之后。必避免多余的数据处理。  
  153. 处理逻辑需要跟在exit when之后。这一点需要多加小心。  
  154. 循环结束后要记得关闭游标。 
  155.  
  156. */ 
  157.  
  158. --方法1:基于表的记录变量接收游标数据 
  159.  
  160.  
  161. create or replace procedure proc_select31 
  162. --(isid in t1.sid%type )  --输入参数 
  163. as 
  164. cursor a is select * from t1 ; 
  165. osname t1%rowtype; 
  166. begin 
  167. open a; 
  168. loop 
  169. fetch a into osname; 
  170. exit when a%notfound; 
  171. dbms_output.put_line( '职工的编号为:'||osname.sid||';'||'的职工姓名为  '||osname.sname ); 
  172. end loop; 
  173. close a; 
  174. end
  175.    
  176.          
  177. set serveroutput on 
  178. exec proc_select31 ; 
  179.  
  180.  
  181. --方法2:基于游标的记录变量接收游标数据 
  182.  
  183.  
  184. create or replace procedure proc_select32 
  185. as 
  186. cursor a is select * from t1 ; 
  187. cur_record a%rowtype; 
  188. begin 
  189. open a; 
  190. loop 
  191. fetch a into cur_record;  
  192. exit when a%notfound; 
  193. dbms_output.put_line( '职工的编号为:'||cur_record.sid||';'||'的职工姓名为  '||cur_record.sname ); 
  194. end loop; 
  195. close a; 
  196. end
  197.    
  198.      
  199. set serveroutput on 
  200. exec proc_select32 ; 
  201.  
  202.  
  203. --方法3:基于集合变量的接收游标数据  
  204.  
  205.  
  206. create or replace procedure proc_select33 
  207. as 
  208. cursor a is select * from t1 ; 
  209. type cur_table_type is table of a%rowtype index by binary_integer; 
  210. cur_table cur_table_type; 
  211. int
  212. begin 
  213. open a; 
  214. loop 
  215. i:=a%rowcount+1; 
  216. fetch a into cur_table(i);  
  217. exit when a%notfound; 
  218. dbms_output.put_line( '职工的编号为:'||cur_table(i).sid||';'||'的职工姓名为  '||cur_table(i).sname ); 
  219. end loop; 
  220. close a; 
  221. end
  222.        
  223. set serveroutput on 
  224. exec proc_select33 ; 
  225.  
  226. ---4.5显示游标(while....loop循环):返回多行多列记录 
  227.  
  228.  
  229. /* 
  230.  
  231. 游标打开后,必须执行一次fetch语句,游标的属性才会起作用。所以使用while 循环时, 
  232. 就需要在循环之前进行一次fetch动作。  
  233. 而且数据处理动作必须放在循环体内的fetch方法之前。循环体内的fetch方法要放在最后。否则就会多处理一次。 
  234. while循环是游标里最复杂的一种. 
  235.  
  236. */ 
  237.  
  238. create or replace procedure proc_select4 
  239. --(isid in t1.sid%type )  --输入参数 
  240. as 
  241. cursor a is select * from t1 ; 
  242. osname t1%rowtype; 
  243. begin 
  244. open a; 
  245. fetch a into osname;  
  246. while a%found loop  --循环之前做个fetch 
  247. dbms_output.put_line( '职工的编号为:'||osname.sid||';'||'的职工姓名为  '||osname.sname ); 
  248. end loop; 
  249. close a; 
  250. end
  251.           
  252. set serveroutput on 
  253. exec proc_select4 ; 
  254.  
  255.  
  256. ---4.6显示游标(for循环)(适合多个记录):返回多行多列记录 
  257.  
  258.  
  259. 游标使用for循环不用openfetchclose关闭游标. 
  260.  
  261.  
  262. --方法1:典型for循环 
  263.  
  264.  
  265. create or replace procedure proc_select5 
  266. as 
  267. cursor a is select * from t1 ; 
  268. begin 
  269. for  res in a loop 
  270. dbms_output.put_line( '职工的编号为:'||res.sid||';'||'的职工姓名为  '||res.sname ); 
  271. end loop; 
  272. end
  273.  
  274. set serveroutput on 
  275. exec proc_select5 ; 
  276.  
  277.  
  278. --方法2:简单for循环 
  279.  
  280.  
  281. create or replace procedure proc_select6 
  282. as 
  283. begin 
  284. for  res in ( select * from t1 ) loop 
  285. dbms_output.put_line( '职工的编号为:'||res.sid||';'||'的职工姓名为  '||res.sname ); 
  286. end loop; 
  287. end
  288.  
  289.  
  290. set serveroutput on 
  291. exec proc_select6 ; 
  292.  
  293.  
  294. ----4.7 ref游标(loop循环) 
  295.  
  296.  
  297. /*** 
  298.  
  299. 怎么使用  REF游标 ? 
  300.  ①声明REF 游标类型,确定REF 游标类型; 
  301.   ⑴强类型REF游标:指定retrun type,REF 游标变量的类型必须和return type一致。 
  302.    语法:Type   REF游标名   IS   Ref Cursor Return  结果集返回记录类型; 
  303.   ⑵弱类型REF游标:不指定return type,能和任何类型的CURSOR变量匹配,用于获取任何结果集。 
  304.    语法:Type   REF游标名   IS   Ref Cursor; 
  305.  ②声明Ref 游标类型变量; 
  306.   语法:变量名  已声明Ref 游标类型; 
  307.    
  308.  ③打开REF游标,关联结果集 ; 
  309.   语法:Open   Ref 游标类型变量   For   查询语句返回结果集; 
  310.    
  311.  ④获取记录,操作记录; 
  312.   语法:Fetch    REF游标名 InTo   临时记录类型变量或属性类型变量列表; 
  313.    
  314.  ⑤关闭游标,完全释放资源; 
  315.   语法:Close   REF游标名; 
  316.  
  317.  
  318. 能够使用ref弱类型REF游标就不要使用强类型REF游标 
  319.  
  320. ***/ 
  321.  
  322. --案例1:ref弱类型游标:loop循环 
  323.  
  324.  
  325. create or replace procedure proc_select8 
  326. choice in varchar2 
  327. as 
  328. TYPE cur IS REF CURSOR;  --声明游标类型为ref 
  329. a cur;     --声明变量为ref游标类型 
  330. osname t1%rowtype; 
  331. begin 
  332. if  choice='full' then 
  333. open a for select * from t1; 
  334. loop 
  335. fetch a into osname;  
  336. exit when a%notfound; 
  337. dbms_output.put_line( '职工的编号为:'||osname.sid||';'||'的职工姓名为  '||osname.sname ); 
  338. end loop; 
  339. elsif choice='top' then 
  340. open a for select * from t1 where rownum<10; 
  341. loop 
  342. fetch a into osname;  
  343. exit when a%notfound; 
  344. dbms_output.put_line( '职工的编号为:'||osname.sid||';'||'的职工姓名为  '||osname.sname ); 
  345. end loop; 
  346. else 
  347.   dbms_output.put_line('请输入正确值full或top!谢谢配合!'); 
  348. return
  349. end if; 
  350. close a; 
  351. end
  352.    
  353.        
  354. set serveroutput on 
  355. exec proc_select8('full') ; 
  356. exec proc_select8('top') ; 
  357.  
  358.  
  359. --案例2:ref强类型游标:loop循环 
  360.  
  361.  
  362. create or replace procedure proc_select9 
  363. as 
  364. TYPE cur IS REF CURSOR RETURN t1%RowType;  --声明游标类型为ref 
  365. a cur;     --声明变量为ref游标类型 
  366. osname t1%rowtype; 
  367. begin 
  368. open a for select * from t1;  
  369. loop 
  370. fetch a into osname; 
  371. exit when a%notfound; 
  372. dbms_output.put_line( '职工的编号为:'||osname.sid||';'||'的职工姓名为  '||osname.sname ); 
  373. end loop; 
  374. close a; 
  375. end
  376.  
  377.  
  378.     
  379. set serveroutput on 
  380. exec proc_select9 ;