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 |
|
lppier
Starting Member
3 Posts |
Posted - 2004-09-14 : 00:39:25
|
| HiI'm trying to create a trigger in my school database project. However, the raiseerror is executed no matter what set of values I have tried.The following are some of the data I have tried to insert into the table.Any help would be greatly appreciated! Thanks!Pier.----------------------------------------------<Query>INSERT INTO TEAMVALUES('Australia','Grp2(p)');<result>Server: Msg 50000, Level 16, State 1, Procedure Duplicate_team, Line 11Each participating country can send only 1 team!!<Query>INSERT INTO TEAMVALUES('Thailand','Grp1(p)');<result>Server: Msg 50000, Level 16, State 1, Procedure Duplicate_team, Line 11Each participating country can send only 1 team!!----------------------------------------------<Trigger code>CREATE TRIGGER Duplicate_teamON TEAMFOR INSERTASIF EXISTS(SELECT 'TRUE'FROM Inserted iWHERE i.Nation=Nation) BEGIN RAISERROR('Each participating country can send only 1 team!!',16,1)ROLLBACK TRANEND----------------------------------------------<TEAM table contents>Nation Grp------ ---Australia Grp1(p)Brazil Grp2(p)China Grp1(p)England Grp2(p)France Grp1(p)Japan Grp1(p)Singapore Grp2(p)Here's the table creation code, just in case.CREATE TABLE TEAM (Nation VARCHAR(25) NOT NULL,Grp VARCHAR(15) NOT NULL,PRIMARY KEY (Nation,Grp),FOREIGN KEY (Nation) References NATIONALITY(Nation)ON DELETE CASCADEON UPDATE CASCADE);---------------------------------------------- |
|
|
shallu1_gupta
Constraint Violating Yak Guru
394 Posts |
Posted - 2004-09-14 : 01:03:41
|
| HiI think there is no need to create a Trigger for that instead you canjust make Nation as Primary key in Table Teams instead of (Nation,Grp) |
 |
|
|
lppier
Starting Member
3 Posts |
Posted - 2004-09-14 : 01:36:22
|
| Thanks for your reply, shallu1_gupta..It is necessary to have (Nation, Grp) as a Primary key for the table because it is linked to other tables in our database using (Nation, Grp).Pier. |
 |
|
|
shallu1_gupta
Constraint Violating Yak Guru
394 Posts |
Posted - 2004-09-14 : 01:59:29
|
| I think this might help youALTER TRIGGER Duplicate_teamON TEAMINSTEAD OF INSERTASIF EXISTS(SELECT 'TRUE'FROM Inserted i,Team tWHERE i.Nation=t.Nation)BEGIN RAISERROR('Each participating country can send only 1 team!!',16,1)ROLLBACK TRANENDELSEBEGININSERT INTO TEAMSelect * from InsertedEND |
 |
|
|
lppier
Starting Member
3 Posts |
Posted - 2004-09-14 : 02:14:28
|
| Thanks a lot, it works!Could you be so kind as to explain why INSTEAD OF INSERTworks andFOR INSERT doesn't ? (I tried both cases)I understand from my book that INSTEAD OF that the trigger is checked before the table constaints are checked, while for FOR is the other way round.Again, thanks for your help!Pier. |
 |
|
|
|
|
|
|
|