存储过程输出在select语句中

时间:2018-08-07 17:47:25

标签: oracle stored-procedures

我正拼命地调用一个oracle存储过程,并使输出看起来像它们来自select语句。

目标是将sql嵌入可以执行sql查询的其他软件中。

这里是我想做的事情的一个示例。

/* P_DELETE_THIS_TEST IS A STORED PROCEDURE WITH 4 VARCHAR2 PARAMETERS DEFINED AS IN OUT VARIABLES */

DECLARE
   char_1       VARCHAR2(255) := 'TEST';
   char_2       VARCHAR2(255) := '';
   char_3       VARCHAR2(255) := '';
   char_4       VARCHAR2(255) := '';

BEGIN
   P_DELETE_THIS_TEST(char_1, char_2, char_3, char_4);
   SELECT char_1, char_2, char_3, char_4 FROM DUAL;
END;

1 个答案:

答案 0 :(得分:0)

您可以尝试使用动态sql。 下面的PL / SQL块包含动态SQL的几个示例:

DECLARE
   sql_stmt    VARCHAR2(200);
   plsql_block VARCHAR2(500);
   emp_id      NUMBER(4) := 7566;
   salary      NUMBER(7,2);
   dept_id     NUMBER(2) := 50;
   dept_name   VARCHAR2(14) := 'PERSONNEL';
   location    VARCHAR2(13) := 'DALLAS';
   emp_rec     emp%ROWTYPE;
BEGIN
   EXECUTE IMMEDIATE 'CREATE TABLE bonus (id NUMBER, amt NUMBER)';
   sql_stmt := 'INSERT INTO dept VALUES (:1, :2, :3)';
   EXECUTE IMMEDIATE sql_stmt USING dept_id, dept_name, location;
   sql_stmt := 'SELECT * FROM emp WHERE empno = :id';
   EXECUTE IMMEDIATE sql_stmt INTO emp_rec USING emp_id;
   plsql_block := 'BEGIN emp_pkg.raise_salary(:id, :amt); END;';
   EXECUTE IMMEDIATE plsql_block USING 7788, 500;
   sql_stmt := 'UPDATE emp SET sal = 2000 WHERE empno = :1
      RETURNING sal INTO :2';
   EXECUTE IMMEDIATE sql_stmt USING emp_id RETURNING INTO salary;
   EXECUTE IMMEDIATE 'DELETE FROM dept WHERE deptno = :num'
      USING dept_id;
   EXECUTE IMMEDIATE 'ALTER SESSION SET SQL_TRACE TRUE';
END;