If I'm creating this as a before insert trigger (ps - I'm new at triggers, feel free to point out better ways to do what I'm doing. I cobbled this together from looking at examples in the Redbook):
I want to make sure that if a course has a MaxAttendee of 20 then once 20 people have signed up, don't allow any others to register.
So as long as Max_enroll doesn't equal CourseCount then the record will be inserted. If they are equal, the insert fails.
Thanks for any pointers/suggestions/answers!
Leslie
Code:
/* Creating SQL trigger LANDREWS.CHECKATTENDANCE */
CREATE TRIGGER LANDREWS.CHECKATTENDANCE
BEFORE INSERT ON LANDREWS.HRPEMPLCRSSCH
REFERENCING NEW N
FOR EACH ROW
MODE DB2ROW
Begin
DECLARE v_CourseCount integer;
DECLARE v_MaxEnroll integer;
DECLARE v_error_msg VARCHAR(256);
--determine number of people enrolled
SELECT COUNT(*) INTO v_CourseCount FROM HRPEMPLCRSSCH WHERE CRSSCHID = n.CRSSCHID;
--determine max attendess
SELECT MAXATTENDEES INTO v_MaxEnroll FROM HRPCRSSCHED WHERE SCHEDULEID = n.CRSSCHID;
If v_MaxEnroll = v_CourseCount then
set v_error_msg = 'Maximum Attendance reached.';
SIGNAL SQLSTATE VALUE 'SA001' SET MESSAGE_TEXT = v_error_msg;
End If;
End;
I want to make sure that if a course has a MaxAttendee of 20 then once 20 people have signed up, don't allow any others to register.
So as long as Max_enroll doesn't equal CourseCount then the record will be inserted. If they are equal, the insert fails.
Thanks for any pointers/suggestions/answers!
Leslie