我有一个PL / SQL程序尚未正常工作,我已经接近它的工作,但只需要一些帮助就能看到我出错的地方。
目前,我有一个程序从员工表中检索最早雇用的员工,然后使用%ROWTYPE将其输入到RECORD中,然后将该信息显示给用户。
一切进展顺利,按照计划,对员工长寿的变量声明有一点问题。我在sysdate中最早的hire_date计算并没有得到结果。我正在使用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;
答案 0 :(得分: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);