Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322

    Unanswered: Trigger not firing

    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?

    Code:
    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-)

    [red]Hint: Want your questions answered fast? Follow the direction in this link[/red]
    http://weblogs.sqlteam.com/brettk/ar...5/25/5276.aspx

    Add yourself!
    http://www.frappr.com/sqlteam
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

  2. #2
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    one thing, you have an ON DELETE trigger and no reference to the deleted virtual table and I believe inserted is empty on delete and populated for ON INSERT and ON UPDATE.
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  3. #3
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    What an idiot...so much for cut and paste

    It should be INSERT ...NOT (for god's sake) DELETE

    What a Maroon
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

  4. #4
    Join Date
    Jan 2003
    Location
    Massachusetts
    Posts
    5,800
    Provided Answers: 11
    Was the lack of the INSERT tag the problem? Or is it something else?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •