改写了网上某位大侠(最开始的源头是哪位没记住)写的禁用或启用oracle数据库所有约束的存储过程,增加了异常控制,以使发生异常时也可以执行下去。
–调用过程:
执行前先 set serveroutput on; ----以便打印出输出
禁用约束检查 exec MANAGE_USER_CONSTRAINTS(‘disable‘,true,true,true);
启用约束 exec MANAGE_USER_CONSTRAINTS(‘enable‘,true,true,true);
CREATE OR REPLACE PROCEDURE MANAGE_USER_CONSTRAINTS(OPERATION VARCHAR2,
FK BOOLEAN DEFAULT TRUE,
PK BOOLEAN DEFAULT TRUE,
UK BOOLEAN DEFAULT TRUE) IS
ST VARCHAR2(255);
errorCode number; --异常编码
errorMsg varchar2(1000); --异常信息
out_return varchar2(1000);
flag varchar2(10);
CURSOR R IS
SELECT TABLE_NAME, CONSTRAINT_NAME
FROM USER_CONSTRAINTS
WHERE CONSTRAINT_TYPE = ‘R‘;
CURSOR P IS
SELECT TABLE_NAME, CONSTRAINT_NAME
FROM USER_CONSTRAINTS
WHERE CONSTRAINT_TYPE = ‘P‘;
CURSOR U IS
SELECT TABLE_NAME, CONSTRAINT_NAME
FROM USER_CONSTRAINTS
WHERE CONSTRAINT_TYPE = ‘U‘;
BEGIN
DBMS_OUTPUT.ENABLE(buffer_size => null) ; ---不限制缓存
IF UPPER(OPERATION) IN (‘DROP‘, ‘DISABLE‘) THEN
IF FK THEN
BEGIN
FOR E IN R LOOP
begin
ST := ‘ALTER TABLE ‘ || E.TABLE_NAME || ‘ ‘ || OPERATION || ‘
CONSTRAINT ‘ || UPPER(E.CONSTRAINT_NAME);
EXECUTE IMMEDIATE (ST);
DBMS_OUTPUT.PUT_LINE(ST);
EXCEPTION
when others then
errorCode := SQLCODE;
errorMsg := SUBSTR(SQLERRM, 1, 200);
flag := ‘false‘;
out_return := ‘flag=‘ || flag || ‘,errorCode=‘ || errorCode || ‘,errorMsg=‘ || errorMsg;
dbms_output.put_line(out_return);
null;
end;
END LOOP;
END;
END IF;
IF PK THEN
BEGIN
FOR E IN R LOOP
begin
ST := ‘ALTER TABLE ‘ || E.TABLE_NAME || ‘ ‘ || OPERATION || ‘
CONSTRAINT ‘ || UPPER(E.CONSTRAINT_NAME);
EXECUTE IMMEDIATE (ST);
DBMS_OUTPUT.PUT_LINE(ST);
EXCEPTION
when others then
errorCode := SQLCODE;
errorMsg := SUBSTR(SQLERRM, 1, 200);
flag := ‘false‘;
out_return := ‘flag=‘ || flag || ‘,errorCode=‘ || errorCode || ‘,errorMsg=‘ || errorMsg;
dbms_output.put_line(out_return);
null;
end;
END LOOP;
END;
BEGIN
FOR E IN P LOOP
begin
ST := ‘ALTER TABLE ‘ || E.TABLE_NAME || ‘ ‘ || OPERATION || ‘
CONSTRAINT ‘ || UPPER(E.CONSTRAINT_NAME);
DBMS_OUTPUT.PUT_LINE(ST);
EXECUTE IMMEDIATE (ST);
EXCEPTION
when others then
errorCode := SQLCODE;
errorMsg := SUBSTR(SQLERRM, 1, 200);
flag := ‘false‘;
out_return := ‘flag=‘ || flag || ‘,errorCode=‘ || errorCode || ‘,errorMsg=‘ || errorMsg;
dbms_output.put_line(out_return);
null;
end;
END LOOP;
END;
END IF;
IF UK THEN
BEGIN
FOR E IN U LOOP
begin
ST := ‘ALTER TABLE ‘ || E.TABLE_NAME || ‘ ‘ || OPERATION || ‘
CONSTRAINT ‘ || UPPER(E.CONSTRAINT_NAME);
EXECUTE IMMEDIATE (ST);
DBMS_OUTPUT.PUT_LINE(ST);
EXCEPTION
when others then
errorCode := SQLCODE;
errorMsg := SUBSTR(SQLERRM, 1, 200);
flag := ‘false‘;
out_return := ‘flag=‘ || flag || ‘,errorCode=‘ || errorCode || ‘,errorMsg=‘ || errorMsg;
dbms_output.put_line(out_return);
null;
end;
END LOOP;
END;
END IF;
ELSIF UPPER(OPERATION) IN (‘ENABLE‘) THEN
IF PK THEN
BEGIN
FOR E IN P LOOP
begin
ST := ‘ALTER TABLE ‘ || E.TABLE_NAME || ‘ ‘ || OPERATION || ‘
CONSTRAINT ‘ || UPPER(E.CONSTRAINT_NAME);
DBMS_OUTPUT.PUT_LINE(ST);
EXECUTE IMMEDIATE (ST);
EXCEPTION
when others then
errorCode := SQLCODE;
errorMsg := SUBSTR(SQLERRM, 1, 200);
flag := ‘false‘;
out_return := ‘flag=‘ || flag || ‘,errorCode=‘ || errorCode || ‘,errorMsg=‘ || errorMsg;
dbms_output.put_line(out_return);
null;
end;
END LOOP;
END;
END IF;
IF FK THEN
BEGIN
FOR E IN P LOOP
begin
ST := ‘ALTER TABLE ‘ || E.TABLE_NAME || ‘ ‘ || OPERATION || ‘
CONSTRAINT ‘ || UPPER(E.CONSTRAINT_NAME);
DBMS_OUTPUT.PUT_LINE(ST);
EXECUTE IMMEDIATE (ST);
EXCEPTION
when others then
errorCode := SQLCODE;
errorMsg := SUBSTR(SQLERRM, 1, 200);
flag := ‘false‘;
out_return := ‘flag=‘ || flag || ‘,errorCode=‘ || errorCode || ‘,errorMsg=‘ || errorMsg;
dbms_output.put_line(out_return);
null;
end;
END LOOP;
END;
BEGIN
FOR E IN R LOOP
begin
ST := ‘ALTER TABLE ‘ || E.TABLE_NAME || ‘ ‘ || OPERATION || ‘
CONSTRAINT ‘ || UPPER(E.CONSTRAINT_NAME);
DBMS_OUTPUT.PUT_LINE(ST);
EXECUTE IMMEDIATE (ST);
EXCEPTION
when others then
errorCode := SQLCODE;
errorMsg := SUBSTR(SQLERRM, 1, 200);
flag := ‘false‘;
out_return := ‘flag=‘ || flag || ‘,errorCode=‘ || errorCode || ‘,errorMsg=‘ || errorMsg;
dbms_output.put_line(out_return);
null;
end;
END LOOP;
END;
END IF;
IF UK THEN
BEGIN
FOR E IN U LOOP
begin
ST := ‘ALTER TABLE ‘ || E.TABLE_NAME || ‘ ‘ || OPERATION || ‘
CONSTRAINT ‘ || UPPER(E.CONSTRAINT_NAME);
DBMS_OUTPUT.PUT_LINE(ST);
EXECUTE IMMEDIATE (ST);
EXCEPTION
when others then
errorCode := SQLCODE;
errorMsg := SUBSTR(SQLERRM, 1, 200);
flag := ‘false‘;
out_return := ‘flag=‘ || flag || ‘,errorCode=‘ || errorCode || ‘,errorMsg=‘ || errorMsg;
dbms_output.put_line(out_return);
null;
end;
END LOOP;
END;
END IF;
ELSE
DBMS_OUTPUT.PUT_LINE(‘THE FIRST PARAMETER OF THE PROCEDURE MUST BE
DROP OR ENABLE OR DISABLE‘);
END IF;
END;
/