2012-02-01 6 views
0

私たちは従業員のテーブルを持っていると仮定し、私の目標は、それが動作する部署で給与を持っているこの従業員の給与を10%引き上げるdeclare -blog statmentと20それはより多くの給料、 をしていない場合は%iは、コードが書くPL/SQLのブログの文

begin 
update employees e set e.salary=e.salary+e.salary*0.1; 
where e.salary>(select avg(e.salary) from employees e group by e.department_id); 
update employees e set e.salary=e.salary+e.salary*0.1; 
where e.salary<(select avg(e.salary) from employees e group by e.department_id); 



end; 

されるべきだと思うが、それは、エラーの次のリストを返し、

Error starting at line 4 in command: 
begin 
update employees e set e.salary=e.salary+e.salary*0.1; 
where e.salary>(select avg(e.salary) from employees e group by e.department_id); 
update employees e set e.salary=e.salary+e.salary*0.1; 
where e.salary<(select avg(e.salary) from employees e group by e.department_id); 



end; 
Error report: 
ORA-06550: line 3, column 1: 
PLS-00103: Encountered the symbol "WHERE" when expecting one of the following: 

    (begin case declare end exception 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 
06550. 00000 - "line %s, column %s:\n%s" 
*Cause: Usually a PL/SQL compilation error. 
*Action: 

なぜ私を助けてください?、必要なループなステートメントがありますか?

答えて

4

;

update employees e set e.salary=e.salary+e.salary*0.1; 

;

に声明を終了取り外しができますが、あなたは where前にセミコロン( ;)を持っている WHERE

2

でそれを続けます。セミコロンはPL/SQL文を閉じます。

私もあなたはそれが読みやすいと困難間違いを作るために作る方法であなたの文をフォーマットすることを示唆している:もし、それまでに私はそれを行うことができます

update employees e set 
    e.salary=e.salary+e.salary*0.1 -- no semicolon here 
where 
    e.salary > (
     select avg(e.salary) 
     from employees e 
     group by e.department_id 
    ); 
+0

- 他の私が変更されましたが、それは0行が更新されたと書いています、私は最初に更新すると同時に、それも2番目にも更新しようとしますが、おそらくそのような従業員がいないとtotaly更新することはできません? –

+0

あなたは 'select'サブクエリを直接実行し、平均給与を見ることができます。同じ 'where ...'句を使って 'update ... 'の代わりに' employees county(*)を選択して白くすることもできますし、更新がいくつのレコードに影響を与えるか見ることができます。 – 9000