2011-12-15 6 views
0

感謝を表示したり、表示されていない、私はプロジェクトの数を計算手順を記述する必要がありPLSQL新しい給与はアイデアのために、間違った

、および従業員IDが パラメータとして渡され、従業員の 平均労働時間プロシージャに。平均勤務時間が 10未満の場合、従業員の給与は変わりません。そうでない場合は、 が4未満、給与の5%、給与の10%が給与に加算されます。 。

Employee ID: 101 
Employee Name: Marlen 
Number of projects: 3 
Average Working Hours: 87 
Old Salary: 39206 
New Salary: 41166 

:ここ

create or replace procedure view_data(p_id number) is 
cursor c1 
is 
    select count(distinct a.projectid) as PRJ_COUNT, e.empid, e.empname, e.salary as old_sal, round(avg(a.hours),0)as AVG_HOURS 
    from assignment a join employee e 
    on a.empid=e.empid 
    where e.empid=p_id 
    group by e.empid,e.empname, e.salary; 
creader c1%rowtype; 
cursor c2 
is 
    select empid, salary as new_sal 
    from employee 
    where empid=p_id for update of salary; 
rate_rec c2%rowtype; 

v_new_salary number; 
begin 
    --open c1; 
    --fetch c1 into creader; 
    for creader in c1 loop 

    for rate_rec in c2 loop 
    if creader.avg_hours < 10 then 
     update employee set salary=rate_rec.new_sal 
     where empid=rate_rec.empid; 

    elsif creader.prj_count<4 then 
     update employee set salary=rate_rec.new_sal+rate_rec.new_sal*0.05 
     where empid=rate_rec.empid; 

    else 
     update employee set salary=rate_rec.new_sal+rate_rec.new_sal*0.1 
     where empid=rate_rec.empid; 

    end if; 
    select salary into v_new_salary from employee 
    where empid=creader.empid; 

    dbms_output.put_line('Employee ID: '||creader.empid); 
    dbms_output.put_line('Employee Name: '||creader.empname); 
    dbms_output.put_line('Number of projects: '||creader.prj_count); 
    dbms_output.put_line('Average Working Hours: '||creader.avg_hours); 
    dbms_output.put_line('Old Salary: '||rate_rec.new_sal); 
    dbms_output.put_line('New Salary: '||v_new_salary); 

    end loop; 
end loop; 



    end view_data; 
/

が出力されます:PLSQL文で私の出力には表示されません 新しい給与ここ

と間違っ

何かが私のPLSQLプロシージャですカーソルを使用したソリューション:

create or replace procedure view_data(p_id number) is 
cursor c1 
is 
    select count(distinct a.projectid) as PRJ_COUNT, e.empid, e.empname, e.salary as old_sal, round(avg(a.hours),0)as AVG_HOURS 
    from assignment a join employee e 
    on a.empid=e.empid 
    where e.empid=p_id 
    group by e.empid,e.empname, e.salary; 
creader c1%rowtype; 
cursor c2 
is 
    select empid, salary as new_sal 
    from employee 
    where empid=p_id for update of salary; 
rate_rec c2%rowtype; 

v_new_salary number; 
v_bonus number; 
begin 
    for creader in c1 loop 
    for rate_rec in c2 loop 
    if creader.avg_hours >= 10 then 
     if creader.prj_count<4 then 
      update employee set salary=salary*1.05 
      where empid=rate_rec.empid 
      return salary into v_new_salary; 

    else 
      update employee set salary=salary*1.1 
      where empid=rate_rec.empid 
      returning salary into v_new_salary; 

     end if; 
    end if; 
    v_bonus:=v_new_salary-rate_rec.new_sal; 

    dbms_output.put_line('Employee ID: '||creader.empid); 
    dbms_output.put_line('Employee Name: '||creader.empname); 
    dbms_output.put_line('Number of projects: '||creader.prj_count); 
    dbms_output.put_line('Average Working Hours: '||creader.avg_hours); 
    dbms_output.put_line('Old Salary: '||creader.old_sal); 
    dbms_output.put_line('Bonus: '||v_bonus); 
    dbms_output.put_line('New Salary: '||v_new_salary); 

    end loop; 
end loop; 



    end view_data; 
/

答えて

1

変数rate_recは、外側ではなくループの範囲にあります。あなたはあなたのすべてのput_lineをループの内側に置くべきです。

rate_rec.new_salは古い給与です。新しいものは、以前の給料のレコードではなく、テーブルで更新されます。

あなたの更新を行い、もう一度選択して変更が加えられたことを確認することを提案します。更新ではreturning句を使用して新しい給与を得ることもできます。

可能な解決策は次のとおりです。私は完全にクエリをチェックしていない。手順の最後にcommitがあるはずです。

set serveroutput on 

create or replace procedure update_data(p_id number) is 
prj_count integer; 
avg_hours integer; 
old_salary number; 
new_salary number; 
begin 
    select count(a.projectid), round(avg(a.hours),0), e.salary 
    into prj_count, avg_hours, old_salary 
    from assignment a join employee e on a.empid=e.empid 
    where e.empid=p_id 
    group by e.salary; 

    new_salary := old_salary; 

    if avg_hours >= 10 then 
     if prj_count<4 then 
      update employee set salary=salary+salary*0.05 
      where empid=p_id 
      returning salary into new_salary; 
     else 
      update employee set salary=salary+salary*0.1 
      where empid=p_id 
      returning salary into new_salary; 
     end if; 
    end if; 
    dbms_output.put_line('Old salary: ' || old_salary); 
    dbms_output.put_line('New salary: ' || new_salary); 
end update_data; 
/
+0

THX、それはまだ私が新しいカーソル – mydreamadsl

+0

をdiplsayingされます。私は古い給料と新しい給料を表示するコードも追加しました。今それはあなたがしたいことをする必要があります – lkuty

+0

を更新した新旧の給与間違っ – mydreamadsl

関連する問題