Plsql PL/SQL过程未按预期工作。真的需要新的想法

Plsql PL/SQL过程未按预期工作。真的需要新的想法,plsql,oracle11g,Plsql,Oracle11g,好的,让我介绍一下主题:我有两个不同的员工表,它们具有不同的字段,我想合并到一个表中。但是我想保留旧表,并不时执行我的过程来更新我的新表 这是我到目前为止得到的代码,它在几个小时的错误后最终编译,但是当我执行我的过程时,我得到消息“找不到条目” 有人看到我犯的错误吗?或者知道问题可能是什么?错误来自您的SELECT INTO语句 BEGIN SELECT INTO xxx EXCEPTION WHEN NO_DATA_FOUND THEN bla bla END; 如果Select int

好的,让我介绍一下主题:我有两个不同的员工表,它们具有不同的字段,我想合并到一个表中。但是我想保留旧表,并不时执行我的过程来更新我的新表

这是我到目前为止得到的代码,它在几个小时的错误后最终编译,但是当我执行我的过程时,我得到消息“找不到条目”


有人看到我犯的错误吗?或者知道问题可能是什么?

错误来自您的
SELECT INTO
语句

BEGIN
 SELECT INTO xxx
EXCEPTION WHEN NO_DATA_FOUND THEN
 bla bla
END;
如果Select into找不到结果,则必须捕获一个异常(不能只测试变量是否为null)

所以我在
SELECT语句中添加了类似的块

BEGIN
 SELECT INTO xxx
EXCEPTION WHEN NO_DATA_FOUND THEN
 bla bla
END;
因此,我播放以更正您的过程,输出的dbms_很少(日志使其简短)

现在我们开始:

create or replace
procedure InsertTables
    is
        cursor employeer
            is select name, birthdate, gender, monthly_income, Employeennr, functions from Employee;
        cursor worker
            is select * from Worker;
        anr number;
        ann varchar2(30);
        ide number;
        bcode number;
        persnr number;
        gendr number;
        begin
            for a in employeer
            loop
                begin
                  select Employeennr 
                  into anr 
                  from PersonalnrTagging 
                  where Employeennr = a.Employeennr;
                exception when no_data_found then 
                    select Personalnr_Seq.nextval into ide from dual;
                    select code into bcode from Function_codes where functions = a.functions;
                    insert into Personal
                    values  (ide, GetSurname(a.name), GetFirstname(a.name), DateToAge(a.birthdate), a.gender,
                            bcode, a.monthly_income * 12);
                    insert into PersonalnrTagging values (ide, a.Employeennr);
                end;

                select code into bcode from Function_codes where functions = a.functions;
                select personalnr into persnr from PersonalnrTagging where Employeennr = a.Employeennr;
                update Personal
                set     name = GetSurname(a.name), firstname = GetFirstname(a.name), age = DateToAge(a.birthdate),gender = a.gender,
                            function_code = bcode, incomePerYear = a.monthly_income * 12
                where personalnr = persnr;
            end loop;
            for b in worker
            loop
                gendr:=null;
                bcode:=null;
                persnr:=null;
                begin
                select gender into gendr from Gender where firstname = b.firstname;
                    exception when no_data_found then
                      dbms_output.put_line('no Gender for firstName '||b.firstname);
                    end;
                    begin
                    select code into bcode from Function_codes where functions = 'Worker';
                    exception when no_data_found then
                      dbms_output.put_line('no code for function Worker');
                    end;
                begin
                    select name into ann from Personal where name = b.name and firstname = b.firstname;
                exception when no_data_found then

                    select Personalnr_Seq.nextval into persnr from dual;


                    if gendr is null or bcode is null then
                     dbms_output.put_line('error in inserting');
                    else
                    insert into Personal values (persnr, b.name, b.firstname, StringToAge(b.birth_month), gendr, bcode, b.incomePerHour * 40 * 49);
                    end if;
                end;
                    begin
                    select personalnr into persnr from Personal where name = b.name and firstname = b.firstname;
                    exception when no_data_found then
                     dbms_output.put_line('no persnr with name '||b.name||' and firstname '|| b.firstname);
                    end;
                    if (gendr is null or bcode is null or persnr is null) then
                       dbms_output.put_line('error in updating');
                    else
                    update Personal
                    set name = b.name, firstname = b.firstname, age = StringToAge(b.birth_month), gender = gendr,
                            function_code = bcode, incomePerYear = b.incomePerHour * 40 * 49
                    where 
                            personalnr = persnr;
                    end if;
            end loop;
        end;
现在我们可以安全地运行它,并得到一些错误消息(我们的消息,而不是Oracle的消息):

我们看到您忘记在function_codes表中插入辅助函数

如果你这么做的话

insert into Function_codes values (02, 'Worker');
你没有任何错误

您可以看到,在个人表格中,所有结果:

…但你们的人都有

-六十四 -75 -十一,


在varchar(87=2087,76=2076)中插入缩短的日期时,您认为仍然存在问题

我认为你需要简化事情,而不是使事情复杂化。要合并两个表中的数据,只需使用CTAS-CREATETABLE as Select。。。或合并或创建一个视图。。。我认为CTA或创建视图可能是您的最佳选择。这些只是一般性的建议。保持简单。
insert into Function_codes values (02, 'Worker');