如何使用脚本在DB2中删除未命名的约束

时间:2016-03-15 09:23:47

标签: sql db2

我有一个受限制的表,我不知道它的名称,因为它是系统生成的,我需要使用脚本删除该约束。

1 个答案:

答案 0 :(得分:2)

将它放在myscript.sql文件中:

CREATE OR REPLACE PROCEDURE DROP_CONSTRAINT()
LANGUAGE SQL
CALLED ON NULL INPUT

BEGIN ATOMIC
    declare myconstraint varchar(128);
    declare stmt varchar(2000);

    --- select your constraint using the table and column names it consists of
    set myconstraint = (select constname from syscat.keycoluse where tabname='MYTABLE' and colname='MYCOL') ;
    set stmt = 'alter table mytable drop constraint '|| myconstraint ;

    prepare s1 from stmt;
    execute s1;
END@

这样称呼:

db2 -td@ -svf "myscript.sql"
db2 "call DROP_CONSTRAINT()"
db2 "drop procedure DROP_CONSTRAINT"