jwd*_*aan 38 oracle plsql stored-procedures
我正在努力将存储过程从SQL服务器转换为Oracle.此存储过程提供直接结果集.我的意思是,如果您在例如Management Studio中调用存储过程,则直接获取结果集.
通过转换为Oracle,我可以解决Oracle中不显示结果集的问题
我在互联网上搜索并看到存储过程应该产生一个REF CURSOR,但我仍然在解决问题,编写一小段代码来获得结果集.
伪代码:
调用存储过程并获取游标使用该游标执行某些操作,以便显示我的结果集
有人有想法吗?
Ton*_*ews 66
在SQL Plus中:
SQL> create procedure myproc (prc out sys_refcursor)
2 is
3 begin
4 open prc for select * from emp;
5 end;
6 /
Procedure created.
SQL> var rc refcursor
SQL> execute myproc(:rc)
PL/SQL procedure successfully completed.
SQL> print rc
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- ----------- ---------- ---------- ----------
7839 KING PRESIDENT 17-NOV-1981 4999 10
7698 BLAKE MANAGER 7839 01-MAY-1981 2849 30
7782 CLARKE MANAGER 7839 09-JUN-1981 2449 10
7566 JONES MANAGER 7839 02-APR-1981 2974 20
7788 SCOTT ANALYST 7566 09-DEC-1982 2999 20
7902 FORD ANALYST 7566 03-DEC-1981 2999 20
7369 SMITHY CLERK 7902 17-DEC-1980 9988 11 20
7499 ALLEN SALESMAN 7698 20-FEB-1981 1599 3009 30
7521 WARDS SALESMAN 7698 22-FEB-1981 1249 551 30
7654 MARTIN SALESMAN 7698 28-SEP-1981 1249 1400 30
7844 TURNER SALESMAN 7698 08-SEP-1981 1499 0 30
7876 ADAMS CLERK 7788 12-JAN-1983 1099 20
7900 JAMES CLERK 7698 03-DEC-1981 949 30
7934 MILLER CLERK 7782 23-JAN-1982 1299 10
6668 Umberto CLERK 7566 11-JUN-2009 19999 0 10
9567 ALLBRIGHT ANALYST 7788 02-JUN-2009 76999 24 10
Run Code Online (Sandbox Code Playgroud)
我的解决方案是创建一个管道函数。优点是查询可以是单行:
select * from table(yourfunction(param1, param2));要定义该函数,您需要执行如下操作:
-- Declare the record columns
TYPE your_record IS RECORD(
my_col1 VARCHAR2(50),
my_col2 varchar2(4000)
);
TYPE your_results IS TABLE OF your_record;
-- Declare the function
function yourfunction(a_Param1 varchar2, a_Param2 varchar2)
return your_results pipelined is
rt your_results;
begin
-- Your query to load the table type
select s.col1,s.col2
bulk collect into rt
from your_table s
where lower(s.col1) like lower('%'||a_Param1||'%');
-- Stuff the results into the pipeline..
if rt.count > 0 then
for i in rt.FIRST .. rt.LAST loop
pipe row (rt(i));
end loop;
end if;
-- Add more results as you please....
return;
end find;
Run Code Online (Sandbox Code Playgroud)
如上所述,要查看结果,您所需要做的就是:
select * from table(yourfunction(param1, param2)) t order by t.my_col1;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
128659 次 |
| 最近记录: |