Update trigger fire on Specific column change.

Analyst_SQL 3,576 Reputation points
2023-03-22T07:26:44.6533333+00:00

Below trigger ,i am using for updating record,issue is that , when i change codeitem value,trigger also get fire ,which should not be,becasue update trigger i just applied on PID,if value get change then trigger get fire,so is this possible

Create table #Probale (Prdno int,Codeitem int,P_Qty int,PID int)
Create table #tbl_PackDetail (PDID int,Prdno int,Codeitem int,Qty int,PID int)


insert into #Probale values(10004,1,1,4545)


insert into #tbl_PackDetail values(10001,10004,1,1,4545)



ALTER TRIGGER [dbo].[SBDeletePackdetail] ON [dbo].[tbl_PckDetail] AFTER Update AS
UPDATE #Probale
SET    PID = 1
FROM   Probale G
JOIN   inserted i ON G.Prdno = i.Prdno
where ( G.PID!=1 or G.PID is not null)


SQL Server Reporting Services
SQL Server Reporting Services
A SQL Server technology that supports the creation, management, and delivery of both traditional, paper-oriented reports and interactive, web-based reports.
3,063 questions
SQL Server | Other
0 comments No comments
{count} votes

Accepted answer
  1. Olaf Helper 47,441 Reputation points
    2023-03-22T07:39:06.3966667+00:00

    If you have an UPDATE trigger, then the trigger fires on every update, you can not modify this behaviour.

    But you can use the trigger function UPDATE to check which column data have been updated.


1 additional answer

Sort by: Most helpful
  1. Anonymous
    2023-03-22T08:26:26.8566667+00:00

    Hi @Analyst_SQL

    I added a query to your trigger code to monitor intermediate processes.

    create or alter TRIGGER [dbo].[SBDeletePackdetail] 
    ON tbl_PackDetail 
    AFTER Update AS
    select * FROM Probale G
    JOIN inserted i ON G.Prdno = i.Prdno
    where ( G.PID!=1 or G.PID is not null);
    UPDATE Probale
    SET    PID = 1
    FROM  Probale G
    JOIN inserted i ON G.Prdno = i.Prdno
    where ( G.PID!=1 or G.PID is not null)
    

    Next I run the update statement.

    update  tbl_PackDetail set Codeitem = 2
    

    It displays a table of intermediate procedures.

    User's image

    On the basis of this intermediate process table, the PID is updated.(There are two PID columns on the table, and you do not need to specify G.PID.)

    Finally, check the two original tables.

    User's image

    I guess that's how this trigger works.

    I'm not sure if this is what you want, if there are still questions, please leave a message.

    Best regards,

    Percy Tang


    If the answer is the right solution, please click "Accept Answer". If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.