开发者

Oracle 10g Express: DISABLE CONSTRAINT not really disabling constraints?

开发者 https://www.devze.com 2023-04-01 18:18 出处:网络
I\'ve got a hairy system with almost 650 inter-connected tables (with plenty of bi-directional refs) which is created via Hibernate script. I need to delete left-over (corrupt) data based on an ID whi

I've got a hairy system with almost 650 inter-connected tables (with plenty of bi-directional refs) which is created via Hibernate script. I need to delete left-over (corrupt) data based on an ID which is present in all tables. I've written a script to generate procedures to disable constraints on each of the tables e.g.:

create or replace procedure disable_MyTable as
BEGIN
  FOR r IN (
    SELECT TABLE_NAME, CONSTRAINT_NAME
    FROM USER_CONSTRAINTS WHERE TABLE_NAME = 'MyTable') LOOP
    EXECUTE IMMEDIATE REPLACE(REPLACE(
      'ALTER TABLE #TABLE# DISABLE CONSTRAINT #CON#'
      ,'#TABLE#',r.TABLE_NAME)
      ,'#CON#',r.CONSTRAINT_NAME);
END LOOP;
END;
/

Followed by:

exec disable_MyTable;

Followed by my delete, whereupon I sometimes receive this error:

ORA-02292: integrity constraint (XXX.FK409) violated - child record found

When I check the status of the constraints:

select status from user_constraints where table_name = 'MyTable';

Oracle says all constraints are still ENABLED!

Are procedures being executed in their own instance? Do I need higher privileges?

p.s. I don't care about performance. This 开发者_如何学Pythonis just a clean-up script.


If the statement select status from user_constraints where table_name = 'MyTable' really returns a row, then you need to put double quotes around the #TABLE# placeholder in your ALTER statement. Because in that case MyTable is case sensitive and requires doubles quotes.

You are generating the following statement

ALTER TABLE MyTable DISABLE CONSTRAINT FK409ADEF4HERW;

But it should be:

ALTER TABLE "MyTable" DISABLE CONSTRAINT FK409ADEF4HERW;

Again this assumes that your SELECT statement was using 'MyTable' as shown

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号