Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trigger

Status
Not open for further replies.

postmannie

Technical User
May 14, 2002
3
0
0
GB
Struggling with this one,

Got two tables

Student (national_insurance_nos,name,date_of_birth,room_nos)

ClassRoom (room_nos,builing_name,number_of_seats)

Trigger required for a constraint - number of students cannot be greater than the number of seats in a class room

thanks in advance





 
From Transact SQL Programming by Kline, Gould, and Zanevsky published by O'Reilly, page 495. This is for MS SQL Server.

Code:
CREATE TRIGGER ClassRoomSeats
ON Student
FOR insert
AS
DECLARE @max_seats INT
DECLARE @currently_enrolled INT

SELECT @max_seats = number_of_seats
  FROM ClassRoom
  WHERE inserted.room_nos = ClassRoom.room_nos

SELECT @currently_enrolled = COUNT(*)
  FROM Student
  WHERE Student.room_nos = inserted.room_nos

IF @currently_enrolled + 1 > @max_seats

BEGIN
      ROLLBACK TRIGGER WITH RAISERROR 50002
      "This classroom is full"
END

Please let me know if this helps. I just worked it up from the example in the book.
 
rac2

thanks for the reply, unfortunately it does not work. Using sybase if that helps.

thanks in advance
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top