1

comm が null の場合、comm値をに更新する必要がありますsalary1100、更新されていません。

私のデータは次のとおりです。

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;

これについて意見をください。

4

3 に答える 3

2

この行:

update emp set comm=(select c.sal from emp e where e.comm=c.comm )

... 動作しないでしょう。が null であることはわかっているため、値が一致するc.commのレコードを検索しようとしていますが、 の等値テストは使用できません。複数のレコードがある場合、これも機能しません。その場合、可能な値のどれを使用しますか?empnullcommnullsal

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 でこれを行う必要はまったくありません。単純な SQLupdateで十分です。

于 2013-08-15T17:31:09.430 に答える
1

以下が機能するはずです。

update Employee
set com = sal
where com is null
于 2013-08-15T17:19:24.680 に答える
1

これは単純な更新ステートメントです。

UPDATE YourTable SET comm = sal WHERE comm IS NULL

于 2013-08-15T17:19:24.993 に答える