将参数从一个过程传递到另一个 PLSQL

Pass parameter from one procedure to another PLSQL

我需要将过程“Proc1”的参数“p_date”传递给过程“Proc2”。可能需要使用全局变量或其他方式。感谢您的帮助。

CREATE OR REPLACE PACKAGE PKG_TEST  IS 
 
      PROCEDURE proc1 (p_date DATE );
      PROCEDURE proc2;
 
END;
/
 
CREATE OR REPLACE PACKAGE BODY PKG_TEST IS 
 
PROCEDURE proc1 AS
  BEGIN
  INSERT INTO tbl_ins
  SELECT * FROM tbl WHERE dt = p_date;
  END proc1;
  
PROCEDURE proc2 AS
  BEGIN
    DELETE tbl_ins WHERE dt = p_date;
    
   END proc2;
   
 END PKG_TEST;

重新定义您的包,以便 PROC_2 也采用 DATE 参数:

CREATE OR REPLACE PACKAGE PKG_TEST  IS 
 
      PROCEDURE proc1 (p_date DATE);
      PROCEDURE proc2 (p_date DATE);
 
END;
/
 
CREATE OR REPLACE PACKAGE BODY PKG_TEST IS 
 
PROCEDURE proc1(p_date DATE) AS
BEGIN
  INSERT INTO tbl_ins
    SELECT * FROM tbl WHERE dt = p_date;
END proc1;
  
PROCEDURE proc2(p_date DATE) AS
BEGIN
  DELETE tbl_ins WHERE dt = p_date;
END proc2;
   
END PKG_TEST;

没有真正原因就打包"stateful"不是个好主意。

在您的情况下,如果您要从 proc1 调用 proc2(否则,我不知道您为什么要将其硬编码为与输入参数相同的值proc1), 你可以这样做:

CREATE OR REPLACE PACKAGE PKG_TEST  IS 
    PROCEDURE proc1 (p_date DATE ); 
END;
/
 
CREATE OR REPLACE PACKAGE BODY PKG_TEST IS 
 
  PROCEDURE proc1 IS
    PROCEDURE proc2 IS
    BEGIN -- proc2 is a local procedure of proc1
      DELETE tbl_ins WHERE dt = p_date;
    END proc2;
  BEGIN
    INSERT INTO tbl_ins
    SELECT * FROM tbl WHERE dt = p_date;
    PROC2();
  END proc1;
   
END PKG_TEST;
/