Need Trigger For Update Two table

Analyst_SQL 3,576 Reputation points
2022-03-25T14:44:20.437+00:00

I want to Update table #tbl_BalPacM column Orderno From 15 to 18 by using trigger.
When Trigger is fired ,then it Check table #tbl_BalPacM column PID into #tbl_PckDetail column PID and all prdno which is exist in table #tbl_PckDetail
also exist in #Probale table ,then their Orderno will get change from 15 to 18

Create Table #itemmasterfile(CodeItem int,CID  int,Descriptionitem varchar(50),Packsize varchar(50))   
     CREATE TABLE #Probale (prdno INT,orderno int,CodeItem int,Weigth int,prdqty int,EntryDate date,DelID int,PID int)   
     Create table #tbl_BalPacM (PID int ,Orderno int,del int)  
     Create table #tbl_PckDetail (DID int,PID int, prdno int,delid int)  
             
          
                      
                       
     INSERT INTO #Probale VALUES(10000,15,1,270,1,'2020-10-21',null,111)   
     INSERT INTO #Probale VALUES(10001,15,2,270,1,'2020-10-21',null,111)    
     INSERT INTO #Probale VALUES(10002,15,3,270,1,'2020-10-21',null,111)   
     INSERT INTO #Probale VALUES(10003,15,4,270,1,'2020-10-21',null,111)    
     INSERT INTO #Probale VALUES(10004,16,5,270,1,'2020-10-21',null,112)    
     INSERT INTO #Probale VALUES(10005,16,6,270,1,'2020-10-21',null,112)  
          
          
          
          
  Insert into #tbl_BalPacM values (111,15,null)  
  insert into #tbl_PckDetail values(101,111,10000,null)  
  insert into #tbl_PckDetail values(102,111,10002,null)  
  insert into #tbl_PckDetail values(103,111,10003,null)  
  insert into #tbl_PckDetail values(104,111,10004,null)  

186996-update.jpg

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
SQL Server | Other
0 comments No comments
{count} votes

Answer accepted by question author
  1. Naomi Nosonovsky 8,881 Reputation points
    2022-03-25T15:18:39.417+00:00

    Try this trigger (I could not test as I cannot create triggers on the temporary objects):

    CREATE OR ALTER TRIGGER tr_tbl_BalPacM_upd ON #tbl_BalPacM AFTER UPDATE AS
       BEGIN
           SET NOCOUNT ON;
        ;WITH cte AS (SELECT i.orderno, det.prdno
        FROM Inserted i INNER JOIN #tbl_PckDetail det ON i.PID = det.PID)
    
        MERGE #Probale trg USING cte srce ON trg.prdno = srce.prdno
        WHEN MATCHED AND trg.orderno <> srce.Orderno THEN UPDATE SET
        trg.orderno = srce.Orderno;
    
       END
    
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

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