如何使用select语句作为约束名禁用Oracle中的约束

时间:2017-11-13 11:54:08

标签: oracle oracle11g

我尝试禁用附加到表中列的约束

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');

我尝试上面的陈述。它不起作用。关于如何使其发挥作用的任何想法。

ADV_TEST_COURSE_CREDIT存在于dev,UAT,Production中,因此约束名称不同,但它全部附加了一个名为SEQUENCE_NUMBER的列

1 个答案:

答案 0 :(得分:2)

你需要动态的sql和一些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;