日期:2014-05-16  浏览次数:20398 次

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;

?