Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Creating Triggers

Author  Topic 

eplam2
Starting Member

11 Posts

Posted - 2013-09-10 : 06:06:17
Say for instance I have a table called STUDENTS.
This table will have columns named: Name, Gender, Birthday, Sex, and Year Level.

How do I use a trigger to ensure that the student is NOT above 50 years of age?

Is this right?

CREATE OR REPLACE TRIGGER student_age_constraint
BEFORE INSERT
ON Student
FOR EACH ROW
BEGIN
IF (SELECT COUNT(*) FROM student WHERE birthday=ROUND((SYSDATE-NEW.birthday)/365, 2)) > 50
THEN
Raise_application_error(-30001, 'Invalid Attribute! Any student above the age of 50 is not accepted!');
END IF;
END;

Thanks in Advance!

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-09-10 : 06:16:04
Check the below:
CREATE OR REPLACE TRIGGER student_age_constraint
BEFORE INSERT
ON Student
FOR EACH ROW
BEGIN
IF (ROUND((SYSDATE-NEW.birthday)/365, 2)) > 50 ) THEN
Raise_application_error(-30001, 'Invalid Attribute! Any student above the age of 50 is not accepted!');
END IF;
END;

NOTE:
I doesn't sure about the syntax in Oracle... This is SQL Server Forum
Refer this link
http://docs.oracle.com/cd/E18283_01/appdev.112/e17126/triggers.htm#BCFIDDBB

--
Chandu
Go to Top of Page
   

- Advertisement -