update tabel set sted=kbh, status=7 where kamp = 5
Dette er fejl meddelelsen:
sg 2627, Level 14, State 1, Line 2 Violation of UNIQUE KEY constraint 'IX_addresses_1'. Cannot insert duplicate key in object 'dbo.addresses'. The statement has been terminated.
Sted København eksistere allerede på kamp 5, men så vil jeg bare have den til at opdatere den eksisterende med den nye status.
Har fundet en "on duplucate update...." men denne virker kun til mysql. Er der ikke en lignende til MS SQL?????
Et eksemple jeg har hugget et tilfældigt sted på nettet:
Here's a more detailed example to demonstrate how to use the MERGE statement. This script updates the Stock table based on daily trades tracked in the Trades table.
-- Apply changes to the Stock table based on daily trades -- tracked in the Trades table. Delete a row from the Stock table -- if all the stock has been sold. Update the quantity in the Stock -- table if you still hold some stock after the daily trades. Insert -- a new row if you acquired a new Stock. -- As a result, TXN is deleted, SBUX inserted, MSFT updated
MERGE Stock S -- target table USING Trades T -- source table ON S.Stock = T.Stock WHEN MATCHED AND (Qty + Delta = 0) THEN DELETE -- delete stock if entirely sold WHEN MATCHED THEN -- update stock if you still hold some stock UPDATE SET Qty = Qty + Delta WHEN NOT MATCHED THEN -- insert a row if the stock is newly acquired INSERT VALUES (Stock, Delta) -- output details of INSERT/UPDATE/DELETE operations -- made on the target table OUTPUT $action, inserted.Stock, deleted.Stock;
SELECT * FROM Stock; GO
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.