oracle數據庫存儲過程中的select語句的位置


導讀:在oracle數據庫存儲過程中如果用了select語句,要么使用"select into 變量"語句要么使用游標,oracle不支持單獨的select語句。

先看下這個存儲過程:

create or replace procedure pro_test 
is 
begin 
select * from t_test; 
end pro_test;

這個存儲過程正確嗎?

昨天因為這個,耽誤了好久(在一個存儲過程中用了select語句,但既沒有用游標也沒有用into).

在存儲過程(oracle數據庫)中如果用了select語句,要么使用"select into 變量"語句要么使用游標,oracle不支持單獨的select語句(如表述有誤請指出).

select into 比較簡單,但是如果返回的是一個結果集就無法滿足要求了.

游標分Cursor型游標和SYS_REFCURSOR型游標兩種

Cursor型游標--不能用於參數傳遞

create or replace procedure pro_test() is

cusor_1 Cursor is select 字段名 from 表名 where 條件;

(或者

select class_name into cursor_2 from class where ...;

cursor的另一種用法,需要寫在begin和end之間)

begin

select class_name into cursor_2 from class where ...;

可以使用

for xxx in cursor

loop

....

end loop; --對Cursor進行遍歷

end pro_test;

SYS_REFCURSOR型游標

create or replace procedure pro_test(rsCursor out SYS_REFCURSOR) is

cursor SYS_REFCURSOR;

name varhcar(20);

begin

open cursor for

select name from student where ...; --使用open來打開進行賦值

--遍歷

loop

fetch cursor into name --fetch into來打開遍歷的每條數據

exit when cursor%NOTFOUND; --未找到記錄信息

dbms_output.putline(xxxx);

end loop;

rsCursor := cursor;

end pro_test;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM