In your function you return third input parameter totalhours
as a result return(totalhours);
. Is it ok? I guess that you want to return value of totalhrs
My procedures update statement is leaving a null value in my table
문제
I am trying to write a function that retrieves the number of hours that a user defined consultant has been assigned to a user defined project and the value should be returned to the plsql anonymous block calling the function. My procedure should update the number of hours that the consultant has been assigned to the project.
UPDATE: ok so I edited my code and finally got the function to return the correct value. Now all I need is to call my procedure to actually update the tables. The IF statement in my anonymous block doesnt seem to recognize the newtotal_hours. Any ideas???
NEW UPDATE ok so I got everything compiling and returning as it should be however the only problem I have left is that my procedure's update statement is causing a null value in the table where the updated newtotal_hours should replace it how can I fix this???
Here is my table:
project_consultant
(p_id number(6),
c_id number(6),
total_hours number(6)
);
and here is my WORKING UPDATED FUNCTION:
create or replace function return_num_hours (pid number, cid number)
return number is
totalhrs project_consultant.total_hours%type;
cursor c1 is select total_hours from project_consultant where p_id=pid and c_id=cid;
begin
open c1;
fetch c1 into totalhrs;
close c1;
return totalhrs;
end;
/
and finally heres my procedure:
create or replace procedure update_hours (proj_id number, consult_id number) is
change_in_hours number(6);
project_id project_consultant.p_id%type;
consultant_id project_consultant.c_id%type;
totalhours project_consultant.total_hours%type;
cursor c2 is select p_id, c_id, total_hours from project_consultant where p_id=proj_id and
c_id=consult_id;
begin
open c2;
fetch c2 into project_id, consultant_id, totalhours;
totalhours := totalhours + change_in_hours;
update project_consultant set total_hours = totalhours where p_id = proj_id and c_id = consult_id;
--commit;
close c2;
end;
/
my user-defined anonymous block to call the function and procedure..
declare
totalnumhours number(6);
change_in_hours number(6);
newtotal_hours number(6);
project_id number(6);
consultant_id number(6);
begin
consultant_id := &consult_id;
project_id := &proj_id;
change_in_hours := &change_in_hours;
totalnumhours := return_num_hours(project_id, consultant_id);
dbms_output.put_line(totalnumhours);
newtotal_hours := totalnumhours + change_in_hours;
dbms_output.put_line(newtotal_hours);
if newtotal_hours > 0 then
update_hours(project_id, consultant_id);
dbms_output.put_line('Consultant ' ||consultant_id||' Project '||project_id||':'||' changed planned hours from '||totalnumhours||'
to '||newtotal_hours);
else
dbms_output.put_line('Cannot update number of hours to a negative total');
end if;
commit;
exception
when no_data_found then
dbms_output.put_line('No such consultant or project');
end;
/
script output for consultant_id:101 project_id:1
SQL> select * from project_consultant
P_ID C_ID TOTAL_HOURS
---------- ---------- -----------
1 101
1 104 245
1 103 50
1 105 45
2 105 25
2 100 0
3 103 125
3 104 50
4 105 25
4 104 125
4 102 30
P_ID C_ID TOTAL_HOURS
---------- ---------- -----------
5 105 15
5 103 15
6 103 5
6 104 10
7 102 125
7 100 100
17 rows selected.
해결책