选择每个员工的到期日期

时间:2012-02-25 09:22:24

标签: oracle plsql

假设雇员的到期日期定义为hire_date之后一年。我创建了一个试图完成这项任务的函数:

 create  or   replace   expiredate(empno  in number) return date is
    hiredate  employees.hire_date%type;
begin
    select  add_months(e.hire_date,12) into hiredate
    from employees e
    where empno is not null;
    return   hiredate;
    end  expiredate;

但是它显示了许多错误,例如:

Error starting at line 1 in command:
create  or   replace   expiredate(empno  in number) return date is
hiredate  employees.hire_date%type
Error at Command Line:1 Column:23
Error report:
SQL Error: ORA-00922: missing or invalid option
00922. 00000 -  "missing or invalid option"
*Cause:    
*Action:

Error starting at line 3 in command:
begin
select  add_months(e.hire_date,12) into hiredate
from employees e
where empno is not null;
return   hiredate;
end  expiredate;
Error report:
ORA-06550: line 4, column 7:
PL/SQL: ORA-00904: "EMPNO": invalid identifier
ORA-06550: line 2, column 1:
PL/SQL: SQL Statement ignored
ORA-06550: line 5, column 1:
PLS-00372: In a procedure, RETURN statement cannot contain an expression
ORA-06550: line 5, column 1:
PL/SQL: Statement ignored
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

我认为错误的主要思想是太多行错误,但我该如何更正此代码呢?

1 个答案:

答案 0 :(得分:1)

create声明中存在语法错误,而您放置的select没有意义。

尝试以下方法:

create or replace function expiredate(empno  in number)
  return date is
  hiredate  employees.hire_date%type;
begin
    select  add_months(e.hire_date,12) into hiredate
    from employees e
    where e.empno = empno;
    return   hiredate;
end  expiredate;
/

结果:

SQL> create table employees (hire_date date); 

Table created.
SQL> create or replace function expiredate(empno  in number)
return date is
hiredate  employees.hire_date%type;
begin
    select  add_months(e.hire_date,12) into hiredate
    from employees e
    where e.empno = empno;
    return   hiredate;
end  expiredate;
/  2    3    4    5    6    7    8    9   10  

Function created.

SQL> insert into employees values (1, SYSDATE);

1 row created.

SQL> select * from employees;

     EMPNO HIRE_DATE
---------- ------------------
     1 25-FEB-12

SQL> select expiredate(1) from dual;

EXPIREDATE(1)
------------------
25-FEB-13