2017-09-04 101 views
0

我试图创建一个触发器,允许只插入18岁以上的学生的详细信息。如何仅允许插入18岁及以上的学生?

表结构:

create table student(
uidno numeric(6) primary key, 
fname varchar(20), 
lname varchar(20), 
dob date 
); 

这里是我的PL/SQL触发器:

create or replace trigger checkAge 
before insert 
on student 
for each row 

declare 

curr_year int; 
dob_year int; 

begin 

select extract(year from current_date) into curr_year from dual; 
select extract(year from dob) into dob_year from :new where uidno = :new.uidno; 

if curr_year - dob_year < 18 then 
    raise_application_error(-20098,'Not above 18 years of age'); 

end if; 

end; 

/

我不断收到以下错误: 9/1 PL/SQL:SQL语句忽略 9/55 PLS-00049:坏绑定变量'NEW' 9/60 PL/SQL:ORA-00903:无效的表名

在此先感谢您的 帮帮我。如Barbaros所说,

+0

的问题是在这句话强调了*新*:选择提取物(从DOB年)为从'dob_year:新'在哪里uidno =:new.uidno –

+0

@BarbarosÖzhan是的,我想我已经使用了错误的语法。我也尝试使用:'select extract(year from:new.dob)into dob_year from:new' – Dexter

+0

to use“from:new”作为表名是错误的。它不应该是“从学生”? –

回答

0

这就是如何从字段类型日期中提取年份。

dob_year := to_number(to_char(:new.dob,'rrrr')); 

但是,如果你要计算的时代正确的做法是:

age := floor (month_between (sysdate, :new.dob)/12); 
相关问题