Con*_*rix 19 oracle oracle10g oracle-sqldeveloper
在MS SQL Server中,如果我想检查存储过程的结果,我可能会在Management Studio中执行以下操作.
--SQL SERVER WAY
exec sp_GetQuestions('OMG Ponies')
Run Code Online (Sandbox Code Playgroud)
结果窗格中的输出可能如下所示.
ID Title ViewCount Votes
----- ------------------------------------------------- ---------- --------
2165 Indexed View vs Indexes on Table 491 2
5068 SQL Server equivalent to Oracle’s NULLS FIRST 524 3
1261 Benefits Of Using SQL Ordinal Position Notation? 377 2
(3 row(s) affected)
Run Code Online (Sandbox Code Playgroud)
无需编写循环或PRINT语句.
要在Oracle中执行相同的操作,我可能会在SQL Developer中执行以下匿名块
--ORACLE WAY
DECLARE
OUTPUT MYPACKAGE.refcur_question;
R_OUTPUT MYPACKAGE.r_question;
USER VARCHAR2(20);
BEGIN
dbms_output.enable(10000000);
USER:= 'OMG Ponies';
recordCount := 0;
MYPACKAGE.GETQUESTIONS(p_OUTPUT => OUTPUT,
p_USER=> USER,
) ;
DBMS_OUTPUT.PUT_LINE('ID | Title | ViewCount | Votes' );
LOOP
FETCH OUTPUT
INTO R_OUTPUT;
DBMS_OUTPUT.PUT_LINE(R_OUTPUT.QUESTIONID || '|' || R_OUTPUT.TITLE
'|' || R_OUTPUT.VIEWCOUNT '|' || R_OUTPUT.VOTES);
recordCount := recordCount+1;
EXIT WHEN OUTPUT % NOTFOUND;
END LOOP;
DBMS_OUTPUT.PUT_LINE('Record Count:'||recordCount);
CLOSE OUTPUT;
END;
Run Code Online (Sandbox Code Playgroud)
这输出像
ID|Title|ViewCount|Votes
2165|Indexed View vs Indexes on Table|491|2
5068|SQL Server equivalent to Oracle’s NULLS FIRST|524|3
1261|Benefits Of Using SQL Ordinal Position Notation?|377|2
Record Count: 3
Run Code Online (Sandbox Code Playgroud)
所以SQL版本有1行,oracle有18行,输出很难看.如果有很多列和/或数据是数字,它会加剧.
对我来说奇怪的是,如果我在SQL Developer或Management studio中编写此语句...
SELECT
ID,
Title,
ViewCount,
Votes
FROM votes where user = 'OMG Ponies'
Run Code Online (Sandbox Code Playgroud)
结果非常相似.这让我觉得我要么缺少一种技术,要么使用错误的工具.
Ale*_*ole 18
如果GetQuestions是一个函数返回一个refcursor,这似乎是你在SQL Server版本中所拥有的,那么你可能会做这样的事情:
select * from table(MyPackage.GetQuestions('OMG Ponies'));
Run Code Online (Sandbox Code Playgroud)
或者如果您需要在PL/SQL块中,那么您可以在游标中使用相同的选择.
您也可以让函数生成dbms_output语句,以便它们始终可用于调试,尽管这会增加一些开销.
编辑
嗯,不确定cast()返回的refcursor 是否可能是一个可用的类型,除非你愿意在包外面声明你自己的类型(以及那种类型的表).你可以这样做,只是为了转储结果:
create package mypackage as
function getquestions(user in varchar2) return sys_refcursor;
end mypackage;
/
create package body mypackage as
function getquestions(user in varchar2) return sys_refcursor as
r sys_refcursor;
begin
open r for
/* Whatever your real query is */
select 'Row 1' col1, 'Value 1' col2 from dual
union
select 'Row 2', 'Value 2' from dual
union
select 'Row 3', 'Value 3' from dual;
return r;
end;
end mypackage;
/
var r refcursor;
exec :r := mypackage.getquestions('OMG Ponies');
print r;
Run Code Online (Sandbox Code Playgroud)
并且您可以在另一个过程或函数中使用调用的结果; 它只是在PL/SQL之外进行,似乎有点棘手.
编辑添加:使用这种方法,如果它是一个程序,你可以做基本相同的事情:
var r refcursor;
exec mypackage.getquestions(:r, 'OMG Ponies');
print r;
Run Code Online (Sandbox Code Playgroud)