dwhitzzz

Enable all FK of DB

Oct 4th, 2018
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PL/SQL 1.40 KB | None | 0 0
  1. ----------------------------------
  2. ---Enabling of FOREIGN KEYS-----
  3. ----------------------------------
  4.  
  5. DECLARE
  6.   tabella VARCHAR2(50) := '';
  7.   cons VARCHAR2(100) := '';
  8.   own  VARCHAR2(100) := '';
  9.   tipo CHAR(1) := ' ';
  10. BEGIN
  11.  
  12.   FOR i IN (SELECT table_name, constraint_name, owner, constraint_type --disable first the foreign key
  13.   FROM user_constraints
  14.   WHERE --applica solo su Check constraint on a table, Referential integrity
  15.   table_name NOT LIKE 'BIN$%'
  16.   AND (constraint_type ='C' OR constraint_type ='R')
  17.   AND status = 'DISABLED'
  18.   )
  19.   LOOP
  20.     BEGIN
  21.       tabella:=i.table_name;
  22.       cons:=i.constraint_name;
  23.       tipo:=i.constraint_type;
  24.       own:=i.owner;
  25.  
  26.       EXECUTE IMMEDIATE 'alter table ' ||i.owner|| '.' ||i.table_name|| ' enable constraint ' ||i.constraint_name;
  27.     EXCEPTION
  28.         WHEN OTHERS THEN
  29.            BEGIN
  30.              DBMS_OUTPUT.NEW_LINE;
  31.              DBMS_OUTPUT.PUT_LINE('ATTENTION CONSTRAINT ' || cons || ' OF TYPE ' || tipo || ' NOT ENABLED ON TABLE: ' || tabella );
  32.              EXECUTE IMMEDIATE 'alter table ' ||own|| '.'  ||tabella|| ' enable novalidate constraint ' ||cons;
  33.            EXCEPTION
  34.              WHEN OTHERS THEN
  35.                DBMS_OUTPUT.NEW_LINE;
  36.                DBMS_OUTPUT.PUT_LINE('ATTENTION CONSTRAINT ' || cons || ' OF TYPE ' || tipo || ' DISABLED ON TABLE: ' || tabella );
  37.            END;
  38.     END;
  39.   END LOOP i;
  40. END;
  41. /
Advertisement
Add Comment
Please, Sign In to add comment