SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Creating Triggers
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

eplam2
Starting Member

11 Posts

Posted - 09/10/2013 :  06:06:17  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

India
2217 Posts

Posted - 09/10/2013 :  06:16:04  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000