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.
Author |
Topic |
daipayan
Posting Yak Master
181 Posts |
Posted - 2010-12-04 : 02:59:32
|
Hi,I prepared a Trigger for restriction in Update, but it's not working.I am having 3 tables: Category, GuestCategory:---------------------------ID | Name | Seat_Allocated--------------------------- 1 | RED | 3 2 | BLUE | 2---------------------------Guest:---------------------------------ID | Category_ID | Name | Invite--------------------------------- 1 | 1 | John | YES 2 | 2 | Rose | YES 3 | 2 | Alex | YES 4 | 2 | Jim | NO--------------------------------- You can see for Category 2 (BLUE), I invited 2 Guests (Rose, Alex). Now, if I try to update Invite column of Jim from NO to YES, it should RAISERROR as "Cannot Invite More, Seat Filled"For this I written a trigger, but not working:CREATE TRIGGER dbo.trgInvited_UpdateInsertON dbo.GuestAFTER UPDATE, INSERTASIF EXISTS(SELECT TOP 1 c.ID, COUNT(i.invited) AS TOTAL, c.seatFROM dbo.Category as c INNER JOIN INSERTED as i ON c.ID = i.catIDWHERE (i.invited = 'YES')GROUP BY c.ID, c.seatHAVING (COUNT(i.invited) > c.seat))BEGINRAISERROR('Cannot Invite More, Seat Filled', 16, 1)ROLLBACK TRANEND Please Help me on this.Regards,DaipayanSoftware Programmer Application has reported a 'Not My Fault' in module KRNL.EXE in line 0200:103F |
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-12-04 : 03:17:50
|
I think it should be "for trigger" rather than "After Trigger" as per your requirement.Vaibhav TIf I cant go back, I want to go fast... |
|
|
daipayan
Posting Yak Master
181 Posts |
Posted - 2010-12-04 : 04:04:34
|
No Sir,I tried, still it's not working..quote: Originally posted by vaibhavktiwari83 I think it should be "for trigger" rather than "After Trigger" as per your requirement.Vaibhav TIf I cant go back, I want to go fast...
Regards,DaipayanSoftware Programmer Application has reported a 'Not My Fault' in module KRNL.EXE in line 0200:103F |
|
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-12-04 : 04:52:06
|
Change your trigger script Like below - See the red part.CREATE TRIGGER dbo.trgInvited_UpdateInsertON dbo.GuestAFTER UPDATE, INSERTASIF EXISTS(SELECT TOP 1 c.ID, COUNT(i.invited) AS TOTAL, c.seatFROM dbo.Category as c INNER JOIN Guest as i ON c.ID = i.catIDWHERE (i.invited = 'YES')GROUP BY c.ID, c.seatHAVING (COUNT(i.invited) > c.seat))BEGINRAISERROR('Cannot Invite More, Seat Filled', 16, 1)ROLLBACK TRANEND Vaibhav TIf I cant go back, I want to go fast... |
|
|
daipayan
Posting Yak Master
181 Posts |
Posted - 2010-12-04 : 05:14:32
|
Thank You Sir..it's working..But I didn't understand why it did not took the INSERTED??quote: Originally posted by vaibhavktiwari83 Change your trigger script Like below - See the red part.CREATE TRIGGER dbo.trgInvited_UpdateInsertON dbo.GuestAFTER UPDATE, INSERTASIF EXISTS(SELECT TOP 1 c.ID, COUNT(i.invited) AS TOTAL, c.seatFROM dbo.Category as c INNER JOIN Guest as i ON c.ID = i.catIDWHERE (i.invited = 'YES')GROUP BY c.ID, c.seatHAVING (COUNT(i.invited) > c.seat))BEGINRAISERROR('Cannot Invite More, Seat Filled', 16, 1)ROLLBACK TRANEND Vaibhav TIf I cant go back, I want to go fast...
Regards,DaipayanSoftware Programmer Application has reported a 'Not My Fault' in module KRNL.EXE in line 0200:103F |
|
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-12-04 : 05:30:22
|
Because Inserted is a virtual table which keeps records which newly inserted or updated rather than keeping whole table records.And join with one record will not give status of whole table Guest.I hope I am much clear to explain.Vaibhav TIf I cant go back, I want to go fast... |
|
|
daipayan
Posting Yak Master
181 Posts |
Posted - 2010-12-04 : 05:45:48
|
Yes Sir, understand!!Thank YOUquote: Originally posted by vaibhavktiwari83 Because Inserted is a virtual table which keeps records which newly inserted or updated rather than keeping whole table records.And join with one record will not give status of whole table Guest.I hope I am much clear to explain.Vaibhav TIf I cant go back, I want to go fast...
Regards,DaipayanSoftware Programmer Application has reported a 'Not My Fault' in module KRNL.EXE in line 0200:103F |
|
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-12-04 : 05:51:09
|
WelcomeVaibhav TIf I cant go back, I want to go fast... |
|
|
|
|
|
|
|