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