-4

我在这个问题上遇到了一个小问题..我已经做了我所知道的帮助如果你能

a) 创建一个将新记录插入到“历史”表中的过程“insert_History”。
b) 编写一个小型 PL/SQL 程序,该程序调用“insert_History”过程以根据以下给出的事实插入三个记录:

一世。Mark jackop 于 2009 年 1 月 5 日聘用,因为 'SH-CLERK' 于 2009 年 4 月 6 日获得了销售代表的新职位 (Job_ID = 'SA-REP')。假设前一个职位的结束日期是“2009 年 5 月 31 日”。

表:历史---> emp_id、start_date、end_date、job_id、dep_id
表:员工----> emp_id、name、job_id、dep_id

这就是我所做的!

create or replace
procedure insert_History(emp_id in integer , job_id in number) 
is 
begin 
update History
set ?????? = insert into history(.....) 
where employees.emp_id= emp_id;
end;
4

1 回答 1

0

首先,我会将您的工作放入包中,因为这样可以使事情井井有条,更易于阅读/调用,并且让回过头来记住事情的工作方式变得非常容易(因为我在几个月没有看程序后就知道了我会忘记它到底是做什么的。)我可以想象当某人的代码只是堆积成程序而几乎没有解释它是如何工作的时候,它会是一场噩梦。您不会只在一个类中创建一个 java/oop 程序!

另外要注意的是,使用包只是养成习惯的好习惯,并确保您注释掉代码中的废话以解释它的作用。

请注意,如果我错了,请注意并纠正我,指示要您插入而不是更新 3 条记录。此外,它似乎需要的不仅仅是 emp_id 和 job_id(开始和结束日期以及 dep_id。所以这些值需要通过程序获得。

  create or replace
    PACKAGE "history" AS 
       procedure insert_history(pEmp_id integer, pstart_date date, pend_date date, pJob_id number, pdep_id);
    end history;

create or replace 
package body "history" as 
 procedure insert_history(pEmp_id in integer, pstart_date in date, pend_date in date,               pJob_id in number, pdep_id in number)
 is 
 begin
   insert into history(emp_id , start_date , end_date , job_id, dep_id)
   values(pEmp_id, pstart_date, pend_date, pJob_id, pdep_id );
   commit;
 end insert_history;

这就是插入过程。现在你需要一些可以调用这个过程的东西。在这里,我假设您从应用程序或应用程序中的更高级别获取这些日期,因此开始和结束日期以及 emp 名称作为参数传入。所以我们将把它创建为优秀的程序。

create or replace procedure call_insert_history(Pemp_name in varchar2(256), varchar2(256), Pstart_date in date, Pend_date in date)
is
Vemp_id number;
Vdep_id number;
Vjob_id number;
begin
--first get the emp_id, dep_id, job_id
select emp_id, dep_id, job_id into Vemp_id, Vdep_id, Vjob_id from employees where emp_name = Pemp_name;
--now call the insert_history procedure using the obtained variables.
history.insert_history(Vemp_id, Pstart_date, Pend_date, Vjob_id, Vdep_id);
end call_insert_history;

应该就是这样。

笔记:

这只是我最好的猜测,我还没有测试过。我只是从相当模糊的方向上理解我所理解的。如果您没有从应用程序中获取这些值,则必须存储每个值,然后在不使用参数的情况下调用上述过程的其余部分。

希望这有助于或至少为您指明正确的方向!

于 2012-05-17T22:13:30.493 回答