PL / SQL - 尝试创建一个简单的过程

时间:2014-08-23 22:40:57

标签: oracle plsql

已编辑: 我正在尝试创建一个过程,其中Ref Cursor被定义为OUT参数,我的问题是如何在执行它时调出Ref Cursor,例如这里是我想在我的EXEC sql命令中调用它:< / p>

EXEC film_not_in_stock(2,2,vcur);

以下是程序:

CREATE OR REPLACE PROCEDURE film_not_in_stock( p_film_id IN NUMBER, p_store_id IN NUMBER, vcur OUT SYS_REFCURSOR)
IS
     cur sys_refcursor;
     v_cur inventory.inventory_id%TYPE --or the same type the inventory_id column is
     v_cur sys_refcursor;
BEGIN
     OPEN cur FOR SELECT inventory_id
     FROM inventory
     WHERE film_id = p_film_id
     AND store_id = p_store_id
     AND inventory_id NOT IN (SELECT  inventory_in_stock(inventory_id) FROM dual);
     fetch cur into v_cur;
     EXIT WHEN cur%NOTFOUND;
END;
/

我知道程序中也存在拼写错误,如果你能帮我把它弄好,我将不胜感激。

非常感谢!!

汤妮雅

3 个答案:

答案 0 :(得分:2)

尝试

variable p_cursor REFCURSOR;
DECLARE
BEGIN
film_not_in_stock(2,2,:p_cursor);
end;
 /

print p_cursor;

修改程序为

CREATE OR REPLACE PROCEDURE film_not_in_stock (
   p_film_id    IN     inventory.film_id%TYPE,
   p_store_id   IN     inventory.store_id%TYPE,
   vcur            OUT SYS_REFCURSOR)
AS
BEGIN
   OPEN vcur FOR
      SELECT inventory_id
        FROM inventory
       WHERE     film_id = p_film_id
             AND store_id = p_store_id
             AND inventory_id NOT IN
                    (SELECT inventory_in_stock (inventory_id) FROM DUAL);
END;
/

答案 1 :(得分:2)

以下是:

var results refcursor
exec film_not_in_stock(2,2,:results);

答案 2 :(得分:0)

您需要声明一个变量以将光标结果提取到...

像这样......

CREATE OR REPLACE PROCEDURE film_not_in_stock( p_film_id IN NUMBER, p_store_id IN NUMBER, p_film_count OUT NUMBER, cur OUT SYS_REFCURSOR)
IS
     cur sys_refcursor;
     v_cur inventory.inventory_id%TYPE --or the same type the inventory_id column is
BEGIN
     OPEN cur FOR SELECT inventory_id
     FROM inventory
     WHERE film_id = p_film_id
     AND store_id = p_store_id
     AND inventory_id NOT IN (SELECT  inventory_in_stock(inventory_id) FROM dual);
     --SELECT FOUND_ROWS() INTO p_film_count FROM dual; <<----- **This is the line**
     fetch cur into v_cur;
     EXIT WHEN %NOTFOUND;
     --do whatever you want
END;
/