0

Hi I am using Microsoft SQL Server 2019 (RTM-GDR) (KB4583458) - 15.0.2080.9 (X64) Nov 6 2020 16:50:01 Copyright (C) 2019 Microsoft Corporation Express Edition (64-bit) on Windows Server 2019 Standard 10.0 (Build 17763: ) (Hypervisor)

I have two tables currently with the same columns however one contains new data that will be updated every month lets call that Table 1 and then we have table 2 which has data for the year 2019.

I would like to insert new rows into table 2 and update the records if the amount changed as well.

The columns in the table are: Account ID, Account Description , Amount, Time

How do I achieve this? I know I will need to do an update as well as an insert.

SamBoo11
  • 1
  • 1
  • It looks like [`MERGE`](https://learn.microsoft.com/en-us/sql/t-sql/statements/merge-transact-sql?view=sql-server-ver16) statement could be useful here. – Vladimir Baranov Jun 10 '22 at 15:45
  • Merge is the idea (I like to call it upsert in loose terms). Without merge, you can just do an insert where the records don't exist, then update the records (it does no harm to update the records you just inserted, with the same values again). – topsail Jun 10 '22 at 15:49
  • maybe this answer will benefit you https://stackoverflow.com/questions/37189485/update-and-insert-to-one-table-from-another – hamza felix Jun 10 '22 at 15:51
  • This thread seems same as [does-sql-server-offer-anything-like-mysqls-on-duplicate-key-update](https://stackoverflow.com/questions/1197733/does-sql-server-offer-anything-like-mysqls-on-duplicate-key-update) – Vikram Nimbalkar Jun 10 '22 at 15:51

0 Answers0