创建ORCLE存储过程含有一个输出游标参数和输出整型参数(实现对表的分页效果)
CREATE OR REPLACE PROCEDURE proc_searchTab(tablename varchar2,startline number,endline number,emp_cur out
sys_refcursor,counter out NUMBER)
as
str1 varchar2(1024) :='select * from
(select rownum r,e.* from '||tablename||' e) t where t.r between '|| startline || ' and '|| endline;
str2 VARCHAR2(1024) :='SELECT COUNT(1) FROM '||tablename;
BEGIN
open emp_cur for str1;
EXECUTE IMMEDIATE str2 INTO counter;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM);--SQLERRM(返回当前的错误信息文本)
ROLLBACK;
END proc_searchTab;
/
用PLsql块执行上面创建的过程
SET SERVEROUTPUT ON ;
declare
TYPE emp_record is RECORD( --定义一个记录类型
s number,
v_deptno dept.deptno%type, --dept 代表数据库中的表名(即存储过程输入参数表名)
v_dname dept.dname%type,
v_loc dept.loc%type
);
counter number; --定义一个输出参数(表的总记录数)
dept_cur sys_refcursor; --定义一个游标
dept_rr emp_record ;
begin
PROC_SEARCHTAB('dept',2,3,dept_cur,counter);
loop
fetch dept_cur into dept_rr;
exit when dept_cur%notfound;
dbms_output.put_line(dept_rr.s||'---'|| dept_rr.v_deptno);
end loop;
dbms_output.put_line('总行数-->'||counter);
close dept_cur;
end;