打开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来处理。
你上面做了两次操作,第一次设置为not null,第二步又设置为null,所以现在ID是可以为空的,然后你现在又想要把它设置成可以为null就不行了。你的报错不全面,看看我拿到的error message:
01451. 00000 - "column to be modified to NULL cannot be modified to NULL"
*Cause: the column may already allow NULL values, the NOT NULL constraint
is part of a primary key or check constraint.
*Action: if a primary key or check constraint is enforcing the NOT NULL
constraint, then drop that constraint.
所以你现在可以再试试,先把它设为not null,再设为null就不会有错了。
id列你之前不是定义为不允许为null吗。。。