我的过程update语句在我的表中保留一个空值

时间:2014-05-05 20:04:33

标签: sql function plsql procedure

我正在尝试编写一个函数,该函数检索用户定义的顾问已分配给用户定义项目的小时数,并且该值应返回到调用该函数的plsql匿名块。我的程序应该更新顾问被分配到项目的小时数。

更新:确定所以我编辑了我的代码,最后得到了返回正确值的函数。现在我需要的是调用我的过程来实际更新表。我的匿名块中的 IF语句似乎无法识别newtotal_hours 。任何想法???

NEW UPDATE 确定所以我得到了所有编译并返回的内容,但是我应该留下的唯一问题是我的过程的更新语句导致了一个null值更新的newtotal_hours应该替换它的表我该如何解决这个问题?

这是我的表:

project_consultant
(p_id number(6),
c_id  number(6),
total_hours number(6)
);

这是我的工作更新功能:

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;
/   

最后是我的程序:

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;
/

我的用户定义的匿名块来调用函数和过程..

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;
/

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.

1 个答案:

答案 0 :(得分:1)

在您的函数中,您返回第三个输入参数totalhours作为结果return(totalhours);。好吗?我想你想要返回totalhrs

的值