如何使用 RefCursor 返回类型测试 Oracle 存储过程?

发布于 2024-11-25 06:58:35 字数 81 浏览 1 评论 0原文

我正在寻找有关如何在 SQL Developer 或 Embarcardero Rapid XE2 中测试 Oracle 存储过程的良好解释。谢谢。

I'm looking for a good explanation on how to test an Oracle stored procedure in SQL Developer or Embarcardero Rapid XE2. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

不如归去 2024-12-02 06:58:35

类似的东西

create or replace procedure my_proc( p_rc OUT SYS_REFCURSOR )
as
begin
  open p_rc
   for select 1 col1
         from dual;
end;
/

variable rc refcursor;
exec my_proc( :rc );
print rc;

可以在 SQL*Plus 或 SQL Developer 中工作。我没有任何使用 Embarcardero Rapid XE2 的经验,所以我不知道它是否支持这样的 SQL*Plus 命令。

Something like

create or replace procedure my_proc( p_rc OUT SYS_REFCURSOR )
as
begin
  open p_rc
   for select 1 col1
         from dual;
end;
/

variable rc refcursor;
exec my_proc( :rc );
print rc;

will work in SQL*Plus or SQL Developer. I don't have any experience with Embarcardero Rapid XE2 so I have no idea whether it supports SQL*Plus commands like this.

这样的小城市 2024-12-02 06:58:35

像这样的东西可以让您在几乎任何客户端上测试您的过程:

DECLARE 
  v_cur SYS_REFCURSOR;
  v_a   VARCHAR2(10);
  v_b   VARCHAR2(10);
BEGIN
  your_proc(v_cur);

  LOOP
    FETCH v_cur INTO v_a, v_b;
    EXIT WHEN v_cur%NOTFOUND;
    dbms_output.put_line(v_a || ' ' || v_b);
  END LOOP;
  CLOSE v_cur;
END;

基本上,您的测试工具需要支持 SYS_REFCURSOR 变量的定义以及在传递您定义的变量时调用您的过程的能力,然后循环遍历游标结果集。 PL/SQL 可以做到这一切,并且匿名块易于设置和维护,具有相当的适应性,并且对于使用 PL/SQL 的任何人来说都非常可读。

另一种虽然相似的方法是构建一个执行相同操作的命名过程,并假设客户端有一个调试器(如 SQL Developer、PL/SQL Developer、TOAD 等),然后您可以单步执行。

Something like this lets you test your procedure on almost any client:

DECLARE 
  v_cur SYS_REFCURSOR;
  v_a   VARCHAR2(10);
  v_b   VARCHAR2(10);
BEGIN
  your_proc(v_cur);

  LOOP
    FETCH v_cur INTO v_a, v_b;
    EXIT WHEN v_cur%NOTFOUND;
    dbms_output.put_line(v_a || ' ' || v_b);
  END LOOP;
  CLOSE v_cur;
END;

Basically, your test harness needs to support the definition of a SYS_REFCURSOR variable and the ability to call your procedure while passing in the variable you defined, then loop through the cursor result set. PL/SQL does all that, and anonymous blocks are easy to set up and maintain, fairly adaptable, and quite readable to anyone who works with PL/SQL.

Another, albeit similar way would be to build a named procedure that does the same thing, and assuming the client has a debugger (like SQL Developer, PL/SQL Developer, TOAD, etc.) you could then step through the execution.

幽蝶幻影 2024-12-02 06:58:35

在 SQL Developer 中,您可以右键单击包主体,然后选择“运行”。 “运行 PL/SQL”窗口将允许您编辑 PL/SQL 块。单击“确定”将出现一个标题为“输出变量 - 日志”的窗口窗格,其中包含一个输出变量选项卡。您可以在左侧选择输出变量,结果显示在右侧。非常方便快捷。

我已经将 Rapid 与 T-SQL 结合使用,我认为有一些与此类似的东西。

编写您自己的 delcare-begin-end 脚本,在其中循环遍历光标,就像 DCookie 的示例一样,始终是一个很好的练习。它适用于任何东西,并且您会知道您的代码可以工作。

In SQL Developer you can right-click on the package body then select RUN. The 'Run PL/SQL' window will let you edit the PL/SQL Block. Clicking OK will give you a window pane titled 'Output Variables - Log' with an output variables tab. You can select your output variables on the left and the result is shown on the right side. Very handy and fast.

I've used Rapid with T-SQL and I think there was something similiar to this.

Writing your own delcare-begin-end script where you loop through the cursor, as with DCookie's example, is always a good exercise to do every now and then. It will work with anything and you will know that your code works.

吻泪 2024-12-02 06:58:35

在 Toad 10.1.1.8 中,我使用:

variable salida refcursor
exec MY_PKG.MY_PRC(1, 2, 3, :salida)  -- 1, 2, 3 are params
print salida

然后,作为脚本执行。

In Toad 10.1.1.8 I use:

variable salida refcursor
exec MY_PKG.MY_PRC(1, 2, 3, :salida)  -- 1, 2, 3 are params
print salida

Then, Execute as Script.

Hello爱情风 2024-12-02 06:58:35
create or replace procedure my_proc(  v_number IN number,p_rc OUT SYS_REFCURSOR )
as
begin
open p_rc
for select 1 col1
     from dual;
 end;
 /

然后编写一个函数来调用您的存储过程,

 create or replace function my_proc_test(v_number IN NUMBER) RETURN sys_refcursor
 as
 p_rc sys_refcursor;
 begin
 my_proc(v_number,p_rc);
 return p_rc;
 end
 /

然后您可以在 SQLDeveloper 编辑器中运行此 SQL 查询。

 SELECT my_proc_test(3) FROM DUAL;

您将在控制台中看到结果,右键单击它并选择单个记录视图并编辑结果,您可以看到引用光标返回的所有记录。

create or replace procedure my_proc(  v_number IN number,p_rc OUT SYS_REFCURSOR )
as
begin
open p_rc
for select 1 col1
     from dual;
 end;
 /

and then write a function lie this which calls your stored procedure

 create or replace function my_proc_test(v_number IN NUMBER) RETURN sys_refcursor
 as
 p_rc sys_refcursor;
 begin
 my_proc(v_number,p_rc);
 return p_rc;
 end
 /

then you can run this SQL query in the SQLDeveloper editor.

 SELECT my_proc_test(3) FROM DUAL;

you will see the result in the console right click on it and cilck on single record view and edit the result you can see the all the records that were returned by the ref cursor.

屌丝范 2024-12-02 06:58:35

我想这个链接对你来说就足够了。我在寻找执行oracle程序的方法时发现的。

页面链接

简短描述:

--cursor variable declaration 
variable Out_Ref_Cursor refcursor;
--execute procedure 
execute get_employees_name(IN_Variable,:Out_Ref_Cursor);
--display result referenced by ref cursor.
print Out_Ref_Cursor;

I think this link will be enough for you. I found it when I was searching for the way to execute oracle procedures.

The link to the page

Short Description:

--cursor variable declaration 
variable Out_Ref_Cursor refcursor;
--execute procedure 
execute get_employees_name(IN_Variable,:Out_Ref_Cursor);
--display result referenced by ref cursor.
print Out_Ref_Cursor;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文