Developer technologies | Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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)
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