Done with T SQL
## Steps
```SQL
CREATE TABLE dbo.Deliveries (
DelveryId int IDENTITY(0,1) NOT NULL,
ExpectedDeliveryDate datetime NULL,
OrderId int NULL
);
ALTER TABLE dbo.Deliveries ADD CONSTRAINT Deliveries_PK PRIMARY KEY (DelveryId);
INSERT INTO dbo.Deliveries
(ExpectedDeliveryDate, OrderId)
VALUES
('2023-10-12', 322),
('2023-10-13', 121),
('2023-10-12', 611),
('2023-10-12', 774);
```
## Create a backup table to hold the data that will be overwritten
```SQL
SELECT TOP(0) *
INTO ChangedDeliveries
FROM deliveries
UNION ALL
SELECT TOP(0) *
FROM deliveries
```
We use the UNION all to ensure the new table we create: `ChangedDeliveries` does not get the same identity property on the `DelveryId` column, which would block the `OUTPUT INTO` We will do to copy the changed rows in `Deliveries` into `ChangedDeliveries`
## Use INTO in the UPDATE statement to copy overwritten data backup table
1. you need to select the spefic colum names you cant use a `*` star selector
```sql
SELECT Stuff(
(
SELECT ', ' + syscolumns.name
FROM syscolumns
WHERE id=OBJECT_ID('deliveries')
FOR XML PATH('')
), 1, 2, '') AS columnNamesAsQuery
```
``` SQL
UPDATE st
SET st.AmountPerUnit = '3.73'
OUTPUT deleted.id ,deleted.companyid ,deleted.displaydates ,deleted.formulaid
INTO Tmp.GP2672_ChangedSalaryTypes
FROM dbo.SalaryType AS st
WHERE st.AmountPerUnit = '3.70'
AND st.BaseSalaryTypeId = 156
```
## Restore overwritten data from the backup table
https://stackoverflow.com/questions/224732/sql-update-from-one-table-to-another-based-on-a-id-match