简体   繁体   English

如何使用out sys_refcursor参数执行oracle过程?

[英]How to execute an oracle procedure with an out sys_refcursor parameter?

I have a proc in my package body: 我的包体内有一个proc:

create or replace package body MYPACKAGE is

    procedure "GetAllRules"(p_rules     out sys_refcursor)
    is
    begin
        open p_rules for
        select * from my_rules;

    end "GetAllRules";

-- etc

And I'm exposing this in my package spec. 我在包装规格中公开了这一点。

How do I execute this procedure in a new SQL Window in PL SQL Developer (or similar)? 如何在PL SQL Developer(或类似版本)的新SQL窗口中执行此过程?

You can execute the procedure relatively easily 您可以相对轻松地执行该过程

DECLARE 
  l_rc sys_refcursor;
BEGIN
  mypackage."GetAllRules"( l_rc );
END;

Of course, that simply returns the cursor to the calling application. 当然,这只是将光标返回到调用应用程序。 It doesn't do anything to fetch the data from the cursor, to do something with that data, or to close the cursor. 它不执行任何操作来从游标中获取数据,对该数据执行任何操作或关闭游标。 Assuming that your goal is to write some data to dbms_output (which is useful sometimes for prototyping but isn't something that production code should be relying on), you could do something like 假设您的目标是向dbms_output写入一些数据(有时对原型制作很有用,但生产代码不应该依赖该数据),则可以执行以下操作

DECLARE 
  l_rc sys_refcursor;
  l_rec my_rules%rowtype;
BEGIN
  mypackage."GetAllRules"( l_rc );
  LOOP
     FETCH l_rc INTO l_rec;
     EXIT WHEN l_rc%NOTFOUND;

     dbms_output.put_line( <<print data from l_rec>> );
   END LOOP;

   CLOSE l_rc;
END;

If you're really doing something like this with the cursor in PL/SQL, I'd strongly suggest returning a strongly-typed ref cursor rather than a weakly-typed one so that you can declare a record in terms of the cursor's %rowtype rather than forcing the caller to know exactly what type to declare and hoping that the query in the procedure doesn't change. 如果您确实在PL / SQL中使用游标执行了这样的操作,我强烈建议您返回一个强类型的ref游标,而不是弱类型的ref游标,以便您可以根据游标的%rowtype声明一条记录而不是强迫调用者确切知道要声明的类型,并希望过程中的查询不变。 This also requires you to explicitly write code to display the data which gets annoying. 这还需要您显式地编写代码来显示令人讨厌的数据。

If you're using SQL*Plus (or something that supports some SQL*Plus commands), you can simplify things a bit 如果您使用的是SQL * Plus(或支持某些SQL * Plus命令的东西),则可以简化一些操作

VARIABLE rc REFCURSOR;
EXEC mypackage."GetAllRules"( :rc );
PRINT :rc;

As an aside, I'm not a fan of using case-sensitive identifiers. 顺便说一句,我不喜欢使用区分大小写的标识符。 It gets very old to have to surround identifiers like "GetAllRules" with double-quotes every time you want to call it. 每次调用时都必须用双引号将"GetAllRules"类的标识符引起来,这已经很老了。 Unless you have really compelling reasons, I'd suggest using standard case-insensitive identifiers. 除非您有令人信服的理由,否则建议您使用标准的不区分大小写的标识符。 It's perfectly reasonable to capitalize identifiers reasonably in your code, of course, it just doesn't make a lot of sense to go to the effort of forcing them to be case-sensitive in the data dictionary. 在代码中合理地大写标识符是完全合理的,当然,强迫数据字典中的标识符区分大小写并没有多大意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 sys_refcursor 从函数传递给过程中的 sys_refcursor out 参数 - passing sys_refcursor from function to sys_refcursor out parameter in procedure 使用 SYS_REFCURSOR 作为 OUT 参数的 Oracle 过程仅显示旧值 - Oracle procedure with SYS_REFCURSOR as OUT parameter displays only old values oracle-out sys_refcursor参数-如果没有找到结果呢? - oracle - out sys_refcursor parameter - if no result found then? 如何调用以SYS_REFCURSOR作为OUT参数的Oracle函数 - How to call Oracle function which has SYS_REFCURSOR as OUT Parameter 如何在 OUT SYS_REFCURSOR 中返回 null - How to return null in OUT SYS_REFCURSOR Oracle - 从过程中返回 OUT SYS_REFCURSOR 的最佳方法,其中所有行和列都从表中删除 - Oracle - best way to return from a procedure an OUT SYS_REFCURSOR with all the rows and columns deleted from a table 高效的 Oracle PLSQL 存储过程为相似但不同的 SQL 语句返回 sys_refcursor - Efficient Oracle PLSQL stored procedure to return sys_refcursor for similar, but different, SQL statements 循环后如何从存储过程返回SYS_REFCURSOR? - How do I return a SYS_REFCURSOR from a stored procedure after a loop? 如何从oracle PL / SQL集合中选择所有行到SYS_REFCURSOR - How to select all rows from the oracle PL/SQL collection into SYS_REFCURSOR Oracle - 从SYS_REFCURSOR中选择的pl sql - Oracle - pl sql selecting from SYS_REFCURSOR
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM