我有一个SQL脚本要多次执行(必须是可重入的)。
其中一个脚本行是
alter table MATABLE modify (MADATA null);当有效删除表MATABLE的MADATA列的“not null”约束时,此命令工作良好。但是第二次,我得到了一个错误,例如:
Erreur SQL : ORA-01451: colonne à modifier en non renseignée (NULL) ne peut être passée à NULL
01451. 00000 - "column to be modified to NULL cannot be modified to NULL"这是因为约束已经在第一次执行时被删除,并且不再存在。
如何在没有错误的情况下执行同一脚本?也许是通过PL/SQL脚本?
发布于 2017-08-02 07:56:54
在进行更新之前,可以使用数据字典视图user_tab_cols检查表。
declare
lCount number;
begin
select count(*) into lCount from user_tab_cols
where table_name = 'MATABLE'
and column_name = 'MADATA'
and nullable = 'N';
if (lCount = 1) then
execute immediate 'alter table MATABLE modify (MADATA null)';
end if;
end;请注意,user_tab_cols只包含与登录用户相同架构中的表的信息。如果您正在修改其他用户的表,则可以使用all_tab_cols或dba_tab_cols。
另一种选择是使用异常处理程序并丢弃异常,如下所示:
begin
execute immediate 'alter table MATABLE modify (MADATA null)';
exception
when others then
null;
end;发布于 2017-08-02 07:57:42
这是正常的行为。
这种情况可以在两种情况下发生:
1)如果字段列是约束
( 2)如果您已经将字段列定义为允许空值.
https://stackoverflow.com/questions/45454635
复制相似问题