Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 need addition to my trigger

Author  Topic 

Patyk
Yak Posting Veteran

74 Posts

Posted - 2014-11-04 : 13:59:28
I have a trigger to copy a record to another table if order status changes to 4 in Sales Order table. If true the record is copied to ATS table. Some times the Sales Order status is changed back from 4 to 1 and to 4 again.
Before I copy the record to my ATS table I need to find out if the old record with same PickSlip and Account exists. If exist I need to delete it and update with the record from Sales Order table.

IF (UPDATE(OrderStatus))
BEGIN
INSERT INTO Reporting.dbo.ATS__Queue
(Pickslip,
Docket,
Account,
Attention,
[Name],
[Address],
Address2,
City,
Province,
Country,
Zip,
Phone,
)


SELECT RTRIM(REPLACE(SalesOrder,',',' ')),'AGPROFFESS', RTRIM(REPLACE(Customer,',',' ')),' ',
RTRIM(REPLACE(CustomerName,',',' ')),
RTRIM(REPLACE(ShipAddress1,',',' ')),
'',
RTRIM(REPLACE(ShipAddress2,',',' ')),
RTRIM(REPLACE(ShipAddress3,',',' ')),
RTRIM(REPLACE(ShipAddress4,',',' ')),
RTRIM(REPLACE(ShipPostalCode,',',' ')),
RTRIM(REPLACE(ShippingInstrs,',',' ')),

FROM inserted
WHERE OrderStatus = '4' and Branch IN ('AB','BC','SM','IN')

END
   

- Advertisement -