Oracle:推迟 begin/end 块中的约束?

Oracle: Deferring a constraint in a begin/end block?

如何在 begin/end 块中延迟约束?

这个有效:

SQL> set constraint t_pk deferred;                                              
Constraint set.

但是相同的语句在 begin/end 块中失败:

SQL> begin
2  set constraint t_pk deferred;
3  end;
4  /
set constraint t_pk deferred;
    *
ERROR at line 2:
ORA-06550: line 2, column 5:
PL/SQL: ORA-00922: missing or invalid option
ORA-06550: line 2, column 1:
PL/SQL: SQL Statement ignored

您需要使用execute immediate:

begin
execute immediate 'set constraint t_pk deferred';
end;