oracle存儲過程中的select語句
導(dǎo)讀:在oracle數(shù)據(jù)庫存儲過程中如果用了select語句,要么使用"select into 變量"語句要么使用游標(biāo),oracle不支持單獨(dú)的select語句。
先看下這個(gè)存儲過程:
create or replace procedure pro_test
is
begin
select * from t_test;
end pro_test;
這個(gè)存儲過程正確嗎?
昨天因?yàn)檫@個(gè),耽誤了好久(在一個(gè)存儲過程中用了select語句,但既沒有用游標(biāo)也沒有用into).
在存儲過程(oracle數(shù)據(jù)庫)中如果用了select語句,要么使用"select into 變量"語句要么使用游標(biāo),oracle不支持單獨(dú)的select語句(如表述有誤請指出).
select into 比較簡單,但是如果返回的是一個(gè)結(jié)果集就無法滿足要求了.
游標(biāo)分Cursor型游標(biāo)和SYS_REFCURSOR型游標(biāo)兩種
Cursor型游標(biāo)--不能用于參數(shù)傳遞
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進(jìn)行遍歷
end pro_test;
SYS_REFCURSOR型游標(biāo)
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來打開進(jìn)行賦值
--遍歷
loop
fetch cursor into name --fetch into來打開遍歷的每條數(shù)據(jù)
exit when cursor%NOTFOUND; --未找到記錄信息
dbms_output.putline(xxxx);
end loop;
rsCursor := cursor;
end pro_test;
上文就是我要為大家介紹的關(guān)于oracle數(shù)據(jù)庫存儲過程中select語句的全部內(nèi)容,希望大家都能夠從中收獲。
【編輯推薦】




















