我想声明一个选择查询以在触发器中使用它,但是我通常是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;
答案 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。对于这种类型的任务,使用数据库触发器是无关紧要的。