如何在 PL/SQL 中声明我的 select 查询?

How do I declare my select query in PL/SQL?

我想声明一个 select 查询以在触发器中使用它,但我在 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;

您可以使用由 BULK COLLECTFORALL 组成的 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。使用数据库触发器与此类任务无关。