Author |
Topic |
X002548
Not Just a Number
15586 Posts |
Posted - 2007-12-07 : 11:02:41
|
OK, I'm at a loss..it must be staring me right in the face.I have a junction table that relates 2 tables, with a unique key of the composit of the 2 keys. There is also an indicator that says 1 relationship between the 2 tables is "primary" and there should only be one of those. So I figured a trigger to take care of it...but I can't seem to get it working...I wrote sample sql to mimic the inserted table as well, and it seems correct, but the trigger just does not fire.Any ideas?CREATE TABLE [dbo].[PIF_MEP99] ( [PIFRecID] [int] NOT NULL , [MEPRecID] [int] NOT NULL , [PrimaryInd] [char] (1) NOT NULL) ON [PRIMARY]GOALTER TABLE [dbo].[PIF_MEP99] WITH NOCHECK ADD CONSTRAINT [PIF_MEP99_PK] PRIMARY KEY CLUSTERED ( [PIFRecID], [MEPRecID] ) ON [PRIMARY] GOINSERT INTO PIF_MEP99(PIFRecID, MEPRecID, PrimaryInd)SELECT 1,1,'Y' UNION ALLSELECT 2,1,'N' UNION ALLSELECT 3,2,'N'GOCREATE TRIGGER dbo_PIF_MEP99_tr_Rule1 ON dbo.PIF_MEP99 FOR UPDATE, DELETE AS SET NOCOUNT ON -- Rule 1: Prevent and MEP from having more than 1 PIF as Primary IF Exists ( SELECT * FROM inserted i INNER JOIN PIF_MEP99 p ON i.MEPRecID = p.MEPRecID AND i.PrimaryInd = 'Y' AND p.PrimaryInd = 'Y') BEGIN ROLLBACK TRAN RAISERROR 500003 'Attempting to Insert 2 Primary PIFs for an MEP' ENDGOSELECT * FROM PIF_MEP99GO SELECT * FROM (SELECT 4 AS PIFRecID,1 AS MEPRecID,'Y' AS PrimaryInd) AS i INNER JOIN PIF_MEP p ON i.MEPRecID = p.MEPRecID AND i.PrimaryInd = 'Y' AND p.PrimaryInd = 'Y'GOBEGIN TRANINSERT INTO PIF_MEP99 (PIFRecID, MEPRecID, PrimaryInd) SELECT 4,1,'Y'COMMIT TRANGOSELECT * FROM PIF_MEP99GO SELECT * FROM (SELECT 5 AS PIFRecID,1 AS MEPRecID,'Y' AS PrimaryInd) AS i INNER JOIN PIF_MEP p ON i.MEPRecID = p.MEPRecID AND i.PrimaryInd = 'Y' AND p.PrimaryInd = 'Y'GODROP TABLE PIF_MEP99GO Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
|
Van
Constraint Violating Yak Guru
462 Posts |
Posted - 2007-12-07 : 11:08:04
|
At first glance I see this:CREATE TRIGGER dbo_PIF_MEP99_tr_Rule1 ON dbo.PIF_MEP99 FOR UPDATE, DELETE Then I see you doing an INSERT and looking for results. Maybe change it to:CREATE TRIGGER dbo_PIF_MEP99_tr_Rule1 ON dbo.PIF_MEP99 FOR INSERT, UPDATE, DELETE |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-07 : 11:08:22
|
FOR Update and DeleteWouldn't Update and Insert be a better choice? E 12°55'05.25"N 56°04'39.16" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-07 : 11:09:17
|
Ouch me! I think DELETE is not necessary at all here. E 12°55'05.25"N 56°04'39.16" |
 |
|
Van
Constraint Violating Yak Guru
462 Posts |
Posted - 2007-12-07 : 11:13:00
|
Probably not. I just noticed that INSERT was left out. I hate getting hung up on these types of things...usually just have to take a break and come back and look at it. |
 |
|
X002548
Not Just a Number
15586 Posts |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-07 : 11:35:44
|
You're welcome. E 12°55'05.25"N 56°04'39.16" |
 |
|
X002548
Not Just a Number
15586 Posts |
|
X002548
Not Just a Number
15586 Posts |
Posted - 2007-12-07 : 14:01:19
|
Ok, I think I got it figured out...now can this be made more effecient?CREATE TRIGGER dbo_PIF_MEP_tr_Rule1 ON dbo.PIF_MEP FOR UPDATE, INSERT AS SET NOCOUNT ON -- Rule 1: Prevent an MEP from having more than 1 PIF as Primary IF ( SELECT COUNT(*) FROM inserted i INNER JOIN PIF_MEP p ON i.MEPRecID = p.MEPRecID AND i.PrimaryInd = 'Y' AND p.PrimaryInd = 'Y') <> 1 BEGIN ROLLBACK TRAN RAISERROR 500003 'Attempting to Insert 2 Primary PIFs for an MEP' ENDGO Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-07 : 18:17:37
|
Something similar to this?CREATE TRIGGER dbo_PIF_MEP_tr_Rule1ON dbo.PIF_MEPFOR UPDATE, INSERTAS SET NOCOUNT ON -- For UPDATE, if neither MEPRecID nor PrimaryInd is updated then return nowIF NOT (UPDATE(MEPRecID) OR UPDATE(PrimaryInd)) RETURNDECLARE @MaxItems INTSELECT @MaxItems = MAX(e.Primaries)FROM ( SELECT i.MEPRecID, COUNT(*) AS Primaries FROM inserted AS i WHERE i.PrimaryInd = 'Y' GROUP BY i.MEPRecID ) AS eIF @MaxItems IS NULL RETURNELSE IF @MaxItems > 1 BEGIN ROLLBACK TRAN RAISERROR 500003 'Attempting to INSERT/UPDATE two or more primary PIF for a MEP.' RETURN ENDIF EXISTS ( SELECT * FROM inserted AS i INNER JOIN PIF_MEP AS pm ON pm.MEPRecID = i.MEPRecID WHERE i.PrimaryInd = 'Y' AND pm.PrimaryInd = 'Y' AND i.PIFRecID <> pm.PIFRecID ) BEGIN ROLLBACK TRAN RAISERROR 500003 'Attempting to INSERT/UPDATE two or more primary PIF for a MEP.' RETURN ENDELSE RETURN E 12°55'05.25"N 56°04'39.16" |
 |
|
X002548
Not Just a Number
15586 Posts |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-11 : 09:31:27
|
No.DECLARE @Sample TABLE (Grp INT, Preferred INT)INSERT @SampleSELECT 1, 1 UNION ALLSELECT 2, 0 UNION ALLSELECT 3, 0-- CaseSELECT Grp, COUNT(*)FROM @SampleWHERE Preferred = 1GROUP BY GrpORDER BY Grp If the underlying dataset has no records to aggregate, the result is an empty recordset.Change the Preferred value to 0 for Grp 1 and run the query again. E 12°55'05.25"N 56°04'39.16" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-11 : 09:39:27
|
What the check basically does, is that if there are no PrimaryInd = 'Y' update/insert record, continue as usual because then there can't be any conflicts with new data. E 12°55'05.25"N 56°04'39.16" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-11 : 09:42:02
|
And the check also looks for if there are two or more PrimaryInd = 'Y' for any specific MEPRecID value.If there are two or more records with PrimaryInd = 'Y' in the "batch" (update/insert) already, we don't have to join to the target table because we already know that the data has a conflict. E 12°55'05.25"N 56°04'39.16" |
 |
|
|