如何在PL / SQL中声明选择查询?

时间:2018-06-27 06:12:37

标签: oracle select plsql declare

我想声明一个选择查询以在触发器中使用它,但是我通常是sql的菜鸟:P有人可以帮我吗?

我的代码(示例):

DECLARE Primary_Keys VARCHAR(20);
BEGIN
SELECT cons.constraint_type 
FROM all_constraints cons, all_cons_columns cols 
WHERE cols.owner = 'DAB_NAME' 
AND cons.constraint_type = 'P' 
AND cons.constraint_name = cols.constraint_name 
AND cons.owner = cols.owner;
END;

1 个答案:

答案 0 :(得分:0)

您可以使用由 BULK COLLECT FORALL 组成的 PROCEDURE

SQL> set serveroutput on;
SQL> CREATE OR REPLACE PROCEDURE pr_list_constraints(
                              i_owner IN all_cons_columns.owner%TYPE
                              ) 
IS
BEGIN
      DBMS_OUTPUT.PUT_LINE('Constraint Types for '||i_owner);
      DBMS_OUTPUT.PUT_LINE('------------------------------- ');
   FOR constraint_rec
      IN (SELECT distinct cons.constraint_type 
            FROM all_constraints cons, all_cons_columns cols 
           WHERE cols.owner = i_owner --'DAB_NAME' 
           --AND cons.constraint_type = 'P'
             AND cons.constraint_name = cols.constraint_name 
             AND cons.owner = cols.owner)
   LOOP
      DBMS_OUTPUT.PUT_LINE(constraint_rec.constraint_type);
   END LOOP;
END;
/
SQL> var p_owner varchar2(50);
SQL> exec pr_list_constraints(:p_owner);

 Constraint Types for DAB_NAME 
 -------------------------------
 R
 U
 P
 C

P.S。对于这种类型的任务,使用数据库触发器是无关紧要的。