oracle中无法修改为null怎么办?

2024-12-03 00:36:13
推荐回答(1个)
回答1:

打开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
<<
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;  
运行通过

oracle中null值的问题

  • 先建一个用于测试的临时表:T1.表的内容如下:

    with t1 as(select 1 num1 from dual union select null num1 from dual union select 2 num1 from dual) select * from t1;


  • 如果我想找出num1不等于1的记录。该怎么去写sql呢?我尝试这样去写:select * from t1 where num1<>1;会得出什么结果呢?看下图:


  • 再一次的,我把sql这样写:select * from t1 where num1<>1 or num1 is null;再来看下结果:


  • 总结以上结果:NULL是不可以用来做比较的,无论什么值跟NULL作比较都会返回一个FALSE值。所以当记录中有NULL值的话且要处理的话要用is null来处理。