0

I have a question regarding trigger in sql server. So I got this table :

In Status Table

InStatusID Time CarID

1 NULL 10

and another table which has all primary key from other tables (including the one I posted above) :

Car Table

CarID InStatusID OutStatusID ColorID

1 1 2 9

When inserting data for car table, I need to fill them with ID from other tables. However, the GUI from the application didn't need the column for InStatusID to be inserted by the user. My solution is to make a trigger on Car Table Insert to insert new values for In Status table with the CarID i got from inserted, then when inserting for In Status table there will be another trigger that will update the car table's InStatusID on the same CarID.

For now my trigger look like this :

  1. Car Insert Trigger

    CREATE TRIGGER [dbo].[TRG_Cars_InsertStatus] ON [dbo].[Cars.Cars]
    AFTER INSERT  
    AS
    SET ANSI_NULLS ON
    SET QUOTED_IDENTIFIER ON
    SET NOCOUNT ON 
    SET XACT_ABORT ON
    
    BEGIN
    BEGIN TRY
    DECLARE @ID int
    
    SELECT @ID=i.CarID from inserted i
    
    INSERT INTO [dbo].[Cars.InStatuses] ([Time],[Created],[LastModified],[CarID]) values (NULL,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP,@ID)
    INSERT INTO [dbo].[Cars.OutStatuses] ([Time],[Created],[LastModified],[CarID]) values (NULL,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP,@ID)
    
    END TRY
    
    BEGIN CATCH
    SELECT
    ERROR_NUMBER() AS ErrorNumber
    ,ERROR_SEVERITY() AS ErrorSeverity
    ,ERROR_STATE() AS ErrorState
    ,ERROR_PROCEDURE() AS ErrorProcedure
    ,ERROR_LINE() AS ErrorLine
    ,ERROR_MESSAGE() AS ErrorMessage;
    
          IF @@TRANCOUNT > 0
        ROLLBACK TRANSACTION;
    
    END CATCH;
    
    
    END
    
  2. In Status/Out Status Insert Trigger

    CREATE TRIGGER [dbo].[TRG_OutStatus_InsertCarID] ON [dbo].[Cars.OutStatuses]
    AFTER INSERT  
    AS
    SET ANSI_NULLS ON
    SET QUOTED_IDENTIFIER ON
    SET NOCOUNT ON 
    SET XACT_ABORT ON
    
    
    BEGIN TRY
    DECLARE @ID int
    
    SELECT @ID=i.OutStatusID from inserted i
    
    UPDATE [dbo].[Cars.Cars]
    SET OutStatusID=@ID
    
    FROM [dbo].[Cars.Cars] CAR 
    INNER JOIN [dbo].[Cars.OutStatuses] STA
    ON CAR.CarID = STA.CarID
    END
    

The code runs but there is a bug : Every time the car table updated, every row on the car table would be udpated too. I thought I have tackled that problem by using JOIN ON but no success. Could anyone help me?

1
  • inserted can contain 0, 1 or multiple rows. So e.g. SELECT @ID=i.OutStatusID from inserted i is going to pick a random ID from one of the rows (or be NULL) and all other rows will be ignored. Rather than reading into scalar variables, make inserted one of the tables that you JOIN to. Aug 14, 2014 at 8:06

1 Answer 1

0

simply dont put FK InStatusID on table Car. just put FK CarID on InStatusID

you dont need put both of FK on another table because its 1-1 relationship

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.