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.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Trigger not firing EDIT: Because I'm an IDIOT

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]
GO

ALTER TABLE [dbo].[PIF_MEP99] WITH NOCHECK ADD
CONSTRAINT [PIF_MEP99_PK] PRIMARY KEY CLUSTERED
(
[PIFRecID],
[MEPRecID]
) ON [PRIMARY]
GO

INSERT INTO PIF_MEP99(PIFRecID, MEPRecID, PrimaryInd)
SELECT 1,1,'Y' UNION ALL
SELECT 2,1,'N' UNION ALL
SELECT 3,2,'N'
GO

CREATE 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'
END
GO

SELECT * FROM PIF_MEP99
GO

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'
GO

BEGIN TRAN
INSERT INTO PIF_MEP99 (PIFRecID, MEPRecID, PrimaryInd) SELECT 4,1,'Y'
COMMIT TRAN
GO

SELECT * FROM PIF_MEP99
GO

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'
GO

DROP TABLE PIF_MEP99
GO




Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add 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

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-12-07 : 11:08:22
FOR Update and Delete

Wouldn't Update and Insert be a better choice?



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

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"
Go to Top of Page

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.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2007-12-07 : 11:33:46
OMG

What an idiot

Thanks everyone

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

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"
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2007-12-07 : 13:23:49
And to compond matters, every new primary PIF is added, that trigger will ALWAYS fire, unless I make it an AFTER trigger, which I don't want to do

oye



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

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'
END
GO




Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

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_Rule1
ON dbo.PIF_MEP
FOR UPDATE,
INSERT
AS

SET NOCOUNT ON

-- For UPDATE, if neither MEPRecID nor PrimaryInd is updated then return now
IF NOT (UPDATE(MEPRecID) OR UPDATE(PrimaryInd))
RETURN

DECLARE @MaxItems INT

SELECT @MaxItems = MAX(e.Primaries)
FROM (
SELECT i.MEPRecID,
COUNT(*) AS Primaries
FROM inserted AS i
WHERE i.PrimaryInd = 'Y'
GROUP BY i.MEPRecID
) AS e

IF @MaxItems IS NULL
RETURN
ELSE
IF @MaxItems > 1
BEGIN
ROLLBACK TRAN
RAISERROR 500003 'Attempting to INSERT/UPDATE two or more primary PIF for a MEP.'
RETURN
END

IF 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
END
ELSE
RETURN


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2007-12-11 : 09:21:30
Would that ever return null?

Wouldn't it be at least 0 in each case?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-12-11 : 09:31:27
No.
DECLARE	@Sample TABLE (Grp INT, Preferred INT)

INSERT @Sample
SELECT 1, 1 UNION ALL
SELECT 2, 0 UNION ALL
SELECT 3, 0

-- Case
SELECT Grp,
COUNT(*)
FROM @Sample
WHERE Preferred = 1
GROUP BY Grp
ORDER 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"
Go to Top of Page

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"
Go to Top of Page

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"
Go to Top of Page
   

- Advertisement -