0

我有一个用于 PL/SQL 的程序,它还不能正常工作,我已经接近让它工作了,但只需要一点帮助,看看我哪里出错了。

目前,我有一个程序从员工表中检索最早雇用的员工,然后使用 %ROWTYPE 将其输入到 RECORD 中,然后将该信息显示给用户。

一切进展顺利,按计划进行,只是对员工长寿的变量声明有一点问题。我最早从 sysdate 计算的租用日期没有得到结果。我正在使用 TRUNC(MONTHS_BETWEEN) 计算并将其放入声明的记录中。所以在某个地方变量或计算不起作用。

无论如何,主要问题在于 LONGEVITY 计算不起作用,它只是一个年龄计算。这是我应该得到的格式和答案

Longevity: 9113.5 days or 303.8 months

这是我的编码,在我试图使其基本和简单的所有变量、记录和声明之后,它有点复杂:

SET SERVEROUTPUT ON
DECLARE
  TYPE hr_rec IS RECORD
    (
    hr_longevity        NUMBER(4, 1),
    hr_hire_date        employees.hire_date%TYPE,
    hr_rec_row          employees%ROWTYPE
    );
    hr_myrecord      hr_rec;

BEGIN

SELECT *  INTO hr_myrecord.hr_rec_row
 FROM
 (
     SELECT * 
     FROM  employees
     ORDER BY hire_date
 )
 WHERE rownum = 1;

hr_myrecord.hr_longevity := 
    TRUNC(MONTHS_BETWEEN(SYSDATE, hr_myrecord.hr_hire_date), 1);

DBMS_OUTPUT.PUT_LINE('The employee info for Longest Employed is: '
|| hr_myrecord.hr_rec_row.employee_id
|| '  ' || hr_myrecord.hr_rec_row.first_name 
||  '  ' || hr_myrecord.hr_rec_row.last_name );
DBMS_OUTPUT.PUT_LINE('Hire date is:  ' || hr_myrecord.hr_rec_row.hire_date);
DBMS_OUTPUT.PUT_LINE('Email address is:  ' || hr_myrecord.hr_rec_row.email);
DBMS_OUTPUT.PUT_LINE('Phone number is:  ' || hr_myrecord.hr_rec_row.phone_number);
DBMS_OUTPUT.PUT_LINE('Longevity is: ' || hr_myrecord.hr_longevity );
END;
/

这是它给我的:

anonymous block completed
The employee info for Longest Employed is ID#:  100  Steven  King
Hire date is:  17-JUN-87
Email address is:  SKING
Phone number is:  515.123.4567
Longevity is: 

另外,只是为了好玩,这是我文本中的示例,我应该通过它来进行此计算,它不是很有帮助,但它是相似的,只是计算不同。

DECLARE
type t_rec is record
(v_sal number(8),
v_minsal number(8) default 1000,
v_hire_date employees.hire_date%type,
v_rec1 employees%rowtype);
v_myrec t_rec;
BEGIN
v_myrec.v_sal := v_myrec.v_minsal + 500;
v_myrec.v_hire_date := sysdate;
SELECT * INTO v_myrec.v_rec1
FROM employees WHERE employee_id = 100;
DBMS_OUTPUT.PUT_LINE(v_myrec.v_rec1.last_name ||' '||
to_char(v_myrec.v_hire_date) ||' '|| to_char(v_myrec.v_sal));
END;
4

1 回答 1

2

hr_myrecord.hr_hire_date在计算之前未初始化hr_myrecord.hr_longevity
您可以执行以下操作:

hr_myrecord.hr_longevity := 
    TRUNC(MONTHS_BETWEEN(SYSDATE, hr_myrecord.hr_rec_row.hire_date), 1);
于 2013-02-19T07:33:04.550 回答