Help me fix the code

When I execute teh code below, I get error listed below...


BEGIN
for i IN (SELECT owner, table_name, constraint_name
FROM dba_constraints
WHERE constraint_type <>'R'
AND OWNER IN ('BO','CCRC', 'IA', 'MOMENTUM', 'ODS', 'PB', 'UFMS','UF_ODS', 'VSS', 'VSWMAUDIT', 'WFWMAUDIT', 'WMAUDIT', 'WORKFLOW')
AND status = 'ENABLED')
loop
EXECUTE IMMEDIATE 'alter table ' || i.owner ||'.' ||i.table_name|| ' disable constraint ' ||i.constraint_name;
end loop i;
END;
/

BEGIN
*
ERROR at line 1:
ORA-00942: table or view does not exist
ORA-06512: at line 8

However, when I run the embeded SQL Statement it returns 744 rows.  


Kamal AgnihotriAsked:
Who is Participating?
 
mohammadzahidConnect With a Mentor Commented:
Checked your code and it works fine. Do you have DBA_xxx system views privileges granted to your account?
0
 
OP_ZaharinConnect With a Mentor Commented:
hi Kamal,
- i'm suggesting another way by generating a list of 'Alter Table' script by running the following SQL statement.
- from the result it will format the whole command of alter table <owner.tablename> disable constraint <constraintname> and you can run it in any SQL Editor.

SELECT 'ALTER TABLE ' || owner ||'.'|| table_name || ' DISABLE CONSTRAINT ' || constraint_name 
FROM dba_constraints
WHERE constraint_type <>'R'
AND OWNER IN ('BO','CCRC', 'IA', 'MOMENTUM', 'ODS', 'PB', 'UFMS','UF_ODS', 'VSS', 'VSWMAUDIT', 'WFWMAUDIT', 'WMAUDIT', 'WORKFLOW')
AND status = 'ENABLED'

Open in new window

0
 
Kamal AgnihotriAuthor Commented:
Hi MohammadZaid,

I am running the code logged in as sys. The above code does not work for me. It gives yhe error listed above.

However, a similar code, listed below works.

BEGIN
for i IN (SELECT owner, table_name, constraint_name
FROM dba_constraints
WHERE constraint_type ='R'
AND OWNER IN ('BO','CCRC', 'IA', 'MOMENTUM', 'ODS', 'PB', 'UFMS','UF_ODS', 'VSS', 'VSWMAUDIT', 'WFWMAUDIT', 'WMAUDIT', 'WORKFLOW')
AND status = 'ENABLED')
loop
EXECUTE IMMEDIATE 'alter table ' || i.owner ||'.' ||i.table_name|| ' disable constraint ' ||i.constraint_name;
end loop i;
End;
/

The only difference between the two is the WHERE CONSTRAINT_TYPE <> 'R'
0
 
PilouteConnect With a Mentor Commented:
Hi,

It is strange to see you can't modify an object as SYS.
In order to identify the 'bad' object for further investigation, just add an exception catching with display of the object that triggers the error :

DECLARE
   tab_owner varchar2(30);
   tab_name varchar2(30);
   tab_contraint varchar2(30);
BEGIN
for i IN (SELECT owner, table_name, constraint_name
FROM dba_constraints
WHERE constraint_type <>'R'
AND OWNER IN ('BO','CCRC', 'IA', 'MOMENTUM', 'ODS', 'PB', 'UFMS','UF_ODS', 'VSS', 'VSWMAUDIT', 'WFWMAUDIT', 'WMAUDIT', 'WORKFLOW')
AND status = 'ENABLED')
loop
tab_owner:=i.owner;
tab_name:=i.table_name;
tab_constraint:=i.constraint_name;
EXECUTE IMMEDIATE 'alter table ' || i.owner ||'.' ||i.table_name|| ' disable constraint ' ||i.constraint_name;
end loop i;
EXCEPTION
   WHEN OTHERS THEN
      dbms_output.put_line(tab_owner ||'.' ||tab_name||'.' ||tab_constraint)
END;

You should now know which object you can't modify... (like a missing object still referenced somewhere in the catalog)...

Cheers,
P
0
 
yuchingConnect With a Mentor Commented:
pretty strange, do you have schema which are case sensitive.
Perhaps you can try this, add " " in the table name i.e
ALTER TABLE Owner."TableName" Enable Constraint "Constraint_Name"

BEGIN
for i IN (SELECT owner, table_name, constraint_name
FROM dba_constraints
WHERE constraint_type <>'R'
AND OWNER IN ('BO','CCRC', 'IA', 'MOMENTUM', 'ODS', 'PB', 'UFMS','UF_ODS', 'VSS', 'VSWMAUDIT', 'WFWMAUDIT', 'WMAUDIT', 'WORKFLOW')
AND status = 'ENABLED')
loop
EXECUTE IMMEDIATE 'alter table ' || i.owner ||'."' ||i.table_name|| '" disable constraint "' ||i.constraint_name || '"';
end loop i;
END;
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.