Share via

Two trigger use on Update

Analyst_SQL 3,576 Reputation points
2021-10-15T17:20:44.117+00:00

I want to use two trigger on Update in #tbl_BalPacM table,First if Column Orderno Update then below trigger Execute

ALTER trigger [dbo].[mytriggerorderno]  on [dbo].[tbl_BalPacM]
      after update 
      as
      begin
          if update (orderno)
          begin
          update a
          set a.orderno=b.orderno
          from tbl_PckDetail a
          inner join inserted b
          on a.pid=b.pid
          where a.DelID is null 

          update a
          set a.orderno=b.orderno
          from Probale a
          inner join tbl_PckDetail b
          on a.prdno=b.prdno
           inner join inserted c
           on c.pid=b.pid
           where a.DelID is null and b.DelID is null
          end
      end

Second Trigger IF #tbl_BalPacM Column(del) Update with 1 then execute below code,(Note : Below Trigger is not working ,means that when i execute it,then table #tbl_PckDetail delid and table #probale column PID updated with 1.

ALTER trigger [dbo].[mytriggerdel]  on [dbo].[tbl_BalPacM]
      after update 
      as
      begin
          if update (delid)
          begin
          update a
          set a.orderno=b.orderno
          from tbl_PckDetail a
          inner join inserted b
          on a.pid=b.pid
        --  where a.DelID is null 

          update a
          set a.orderno=b.orderno
          from Probale a
          inner join tbl_PckDetail b
          on a.prdno=b.prdno
           inner join inserted c
           on c.pid=b.pid
        --   where  b.DelID is null
          end
      end

Here is my data,

 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,Orderno 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,15,10000,null)
 insert into #tbl_PckDetail values(101,111,15,10002,null)
 insert into #tbl_PckDetail values(101,111,15,10003,null)
 insert into #tbl_PckDetail values(101,111,15,10004,null)
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
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories


Answer accepted by question author

EchoLiu-MSFT 14,626 Reputation points
2021-10-19T06:56:23.793+00:00
Insert into tbl_BalPacM values (111,16,null)  
  
alter trigger [mytriggerdel]    
on [tbl_BalPacM]  
after update   
as  
begin  
if update (del)  
   begin  
   update a  
   set a.orderno=b.orderno  
   from tbl_PckDetail a  
   inner join inserted b  
   on a.pid=b.pid  
   --  where a.DelID is null   
                  
   update a  
   set a.orderno=b.orderno  
   from Probale a  
   inner join tbl_PckDetail b  
   on a.prdno=b.prdno  
   inner join inserted c  
   on c.pid=b.pid  
   --   where  b.DelID is null  
   end  
end  
  
update tbl_BalPacM   
set del=1  
FROM tbl_BalPacM   
where PID=111  
  
SELECT * FROM Probale  

141619-image.png

You write del in the trigger as delid, I modified it to test that the trigger works normally.

Regards
Echo

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Analyst_SQL 3,576 Reputation points
    2021-10-18T21:46:30.983+00:00

    When orderno is updating,then trigger is working fine,but when del is updating then trigger is not fired

    Was this answer helpful?

    0 comments No comments

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.