您的位置:首页 > 数据库 > Oracle

oracle 存储过程 stored procedure 查询一条记录或多条记录

2010-06-04 18:30 363 查看
参考资料:http://reallyafei.javaeye.com/blog/391097

创建基本表

-- Create table

create table USER_INFORMATION

(

P_ID NUMBER,

USER_LOGIN_NAME NVARCHAR2(30)

)

创建包:

create or replace package pack_test is

type cur_test is ref cursor;

end pack_test;

/

--这个不能少呀,加上这个就可以在sql/plus中运行了,这个是结束符号

创建存储过程

create or replace procedure proc_cur(p_id in number,p_cur out pack_test.cur_test)

is

v_sql varchar2(400);

begin

if p_id = 0 then

open p_cur for select * from user_information;

else

v_sql := 'select * from user_information where id =: p_id';

open p_cur for v_sql using p_id;

end if;

end proc_cur;

测试存储过程

-- Test statements here

set serveroutput on

declare

v_id number := 0;

v_row USER_INFORMATION%rowtype; --注意这里是表名

p_cur pack_test.cur_test;

begin

proc_cur(v_id, p_cur);

loop

fetch p_cur into v_row;

exit when p_cur%notfound;

DBMS_OUTPUT.PUT_LINE(v_row.USER_LOGIN_NAME||'='||v_row.P_ID);

end loop;

close p_cur;

end;

/

--语句块结束符号
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: