0

I try to disable a constraint attached to a column in a table

ALTER TABLE
   ADV_TEST_COURSE_CREDIT
DISABLE constraint

name = (SELECT 
    constraint_name
from
    user_constraints natural join 
    user_cons_columns
where 
    table_name = 'ADV_TEST_COURSE_CREDIT' AND 
    column_name = 'SEQUENCE_NUMBER' AND
    constraint_type = 'C');

I try the statement above. It is not working. Any idea on how to make it work.

Table ADV_TEST_COURSE_CREDIT exists in dev, UAT, Production, so the constraint name is different, but it is all attached a column called SEQUENCE_NUMBER

1
  • You're mixing up DDL and DML and it's not going to work that way. You need to use Dynamic SQL in a PL/SQL block Commented Nov 13, 2017 at 12:04

1 Answer 1

3

You need dynamic sql and some plsql:

declare name varchar2(200);
begin
SELECT 
    constraint_name
into name
from
    user_constraints natural join 
    user_cons_columns
where 
    table_name = 'ADV_TEST_COURSE_CREDIT' AND 
    column_name = 'SEQUENCE_NUMBER' AND
    constraint_type = 'C');
execute immediate 'ALTER TABLE  ADV_TEST_COURSE_CREDIT DISABLE CONSTRAINT :1' using name;
end;
Sign up to request clarification or add additional context in comments.

4 Comments

It would surprise me if you could use bind variables i.e. USING ... in such statement.
execute immediate 'ALTER TABLE ADV_TEST_COURSE_CREDIT DISABLE :1' using name; I have issue with the substitution. Error report - ORA-06550: line 12, column 26: PL/SQL: ORA-00933: SQL command not properly ended ORA-06550: line 3, column 1: PL/SQL: SQL Statement ignored 06550. 00000 - "line %s, column %s:\n%s" *Cause: Usually a PL/SQL compilation error. *Action:
Change the last line to: execute immediate 'ALTER TABLE ADV_TEST_COURSE_CREDIT DISABLE CONSTRAINT '||name;
Yeah, sorry. But Rene already showed the fix. I'll correct it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.