In the case when you want to update or insert all the values ββat the same time, not only for one record, I used this fragment
Run update script
UPDATE Table1
SET OPIS = T1.OPIS
FROM
Table1 AS T
INNER JOIN
Table2 AS T1
ON
T.col = T1.col;
Then paste the script
INSERT INTO Table1
SELECT * FROM
(
SELECT T1. * Table2 AS T1
LEFT JOIN Table1 AS T2 ON (T2.col = T1.col)
WHERE T2.col IS NULL
) AS T;
Hope someone found this helpful.
The equivalent of this in MySql (in some cases) looks something like this:
INSERT INTO table (a, b, c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c = c + 1;
Someone may find that this is due to an article in SQL Server INSERT or UPDATE Solutions
Updated version using MERGE (Transact-SQL) :
DECLARE @USER_ID AS INT=76; DECLARE @TYPE AS NVARCHAR(MAX)='set.global'; DECLARE @FKEY AS NVARCHAR(MAX)='21'; DECLARE @DATA AS NVARCHAR(MAX)='test'; begin tran MERGE UserData USING (SELECT @USER_ID, @TYPE, @FKEY, @DATA) AS Source([UserId], [Type], [FKey], [Data]) ON (UserData.[UserId] = Source.[UserId] AND UserData.[Type] = Source.[Type] AND (UserData.[FKey] = Source.[FKey] OR (Source.[FKey] IS NULL AND UserData.[FKey] IS NULL))) WHEN MATCHED THEN UPDATE SET [Data] = Source.[Data] WHEN NOT MATCHED BY TARGET THEN INSERT ([UserId] ,[Type] ,[FKey] ,[Data]) VALUES ( Source.[UserId] ,Source.[Type] ,Source.[FKey] ,Source.[Data]); commit tran
kikea
source share