Hi @Analyst_SQL ,
According to your description, it seems that you only need to replace NOT EXISTS with EXISTS:
Create table #Proble (Prdno int ,PID int)
Create table #tbl_PackDetail (PID int,Codeitem int,qty int, Orderno int,Prdno int )
Insert into #Proble values (10000001,Null)
Insert into #Proble values (10000001,1)
Insert into #Proble values (10000002,1)
SELECT * FROM #Proble
SELECT * FROM #tbl_PackDetail
IF EXISTS
(
SELECT *
FROM #Proble
WHERE Prdno=10000001 and (PID is null or PID=1)
)
BEGIN
UPDATE #Proble
SET PID=111
WHERE Prdno=10000001
BEGIN
INSERT INTO #tbl_PackDetail (PID,Codeitem,QTY,Orderno,Prdno)
VALUES(111,111,1,111,10000001)
END
END
Output:
Regards
Echo