如果comm为null,我需要将comm
值更新为salary
,即1100
,但不会更新。
我的数据是:
sal comm
9000 800
2975 800
3000 800
1100
3000 800
我的代码是:
declare
cursor c3 is select sal,comm,ename from emp where deptno=20
for update of comm;
begin
for c in c3
loop
if c.comm is null
then
update emp set comm=(select c.sal from emp e where e.comm=c.comm )
where current of c3;
end if;
end loop;
end;
请就此发表意见。
答案 0 :(得分:2)
这一行:
update emp set comm=(select c.sal from emp e where e.comm=c.comm )
......不行。您知道c.comm
为空,因此您尝试在emp
上找到具有匹配值的记录,并且您不能对null
使用相等性测试。如果comm
为null
的记录多于一个,那么这也不起作用 - 在这种情况下,它会使用哪个sal
值?
您根本不需要再次查询emp
; select
即使有效,也毫无意义,因为您可以从正在更新的行中获取数据:
update emp set comm = sal
where current of c3;
您还可以通过将光标更改为仅查找if
值来删除null
测试:
cursor c3 is select sal,comm,ename
from emp
where deptno=20
and comm is null
for update of comm;
但正如其他答案已经指出的那样,您根本不需要在PL / SQL中执行此操作,只需要一个简单的SQL update
即可。
答案 1 :(得分:1)
以下内容应该有效:
update Employee
set com = sal
where com is null
答案 2 :(得分:1)
这是一个简单的更新声明。
UPDATE YourTable SET comm = sal WHERE comm IS NULL