Oracle ORA-01451: 要修改為 NULL 的列無法修改為 NULL

來源:互聯網
上載者:User

調試一段程式,遇到如題錯誤,查資料才發現Oracle中不允許將NULL欄位修改為NULL欄位。只好在修改之前做判斷了。
開啟PL/SQL,寫如下代碼
declare
visnull varchar2(4);
begin
select nullable into visnull from user_tab_columns
where table_name = upper('tblStockInspect')
and column_name = upper('FDepartID');
if visnull = 'N' then
alter table tblStockInspect modify FDepartID int null;
end if;
end; 

運行,又出現錯誤提示如下
---------------------------------------------------------------------------
ORA-06550: 第 8 行, 第 7 列:
PLS-00103: 出現符號 "ALTER"在需要下列之一時:
( begin case declare exit
for goto if loop mod null pragma raise return select update
while with <an identifier>
<a double-quoted delimited-identifier> <a bind variable> <<
continue close current delete fetch lock insert open rollback
savepoint set sql execute commit forall merge pipe purge
---------------------------------------------------------------------------------
仔細一看,原來alter不允許在PL/SQL下直接運行,只好更改如下
declare
visnull varchar2(4);
begin
select nullable into visnull from user_tab_columns
where table_name = upper('tblStockInspect')
and column_name = upper('FDepartID');
if visnull = 'N' then
execute immediate 'alter table tblStockInspect modify FDepartID int null‘;
end if;
end; 
運行通過

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.