This is 1/4 of an overall assignment and I need help with this part.
The assignment question:
Create a trigger on the Rental table that will check when a new rental is to be inserted, that the member (MemberId) that is renting a book:
a. Does not have more than 2 books rented out: the books that are rented out are identified by having ReturnDate as NULL.
b. That the member does not owe more than 50 dollars to the library (the BalanceOwing attribute of the Member table).
What I have done so far :
CREATE TRIGGER tr_tblRental_ForInsert
ON dbo.Rental
FOR INSERT
AS
BEGIN
declare @MemberId int
select @MemberId int = MemberId from inserted
I would appreciate tips on how to go about this.
Best Answer
Don't assume that the trigger will always handle a single row inserted in the table. If you're inserting more than one row in
Rental
, the code you attempted to write will fail.The logical tables
inserted
anddeleted
contain all the rows modified by the DML operation and they must be joined or correlated to the base table (or other tables) in order to take all the rows modified into consideration.As far as the business logic required is concerned, this is a possible implementation:
Please do not use this code in your project/homework if you don't understand what it does: your teacher will ask questions and you'd better have answers. If you need help understanding the code, place a comment here.