I have two tables with identical fields, as below.
Table1
| EmployeeNIN | KeySection | Result | Comment | Auditor |
|:--------------: |:---------------:|:------:|:-------:|:-------:|
|CF60012102UJDJ | Vote Name | UBOS | Great | UZ062 |
|CF60012102UJDJ | Employee Number | 78562 | Nice | UZ062 |
|CF60012102UJDJ | Supplier Number | 12556 | Wrong | UZ062 |
Table2
| EmployeeNIN | KeySection | Result | Comment | Auditor |
|:--------------: |:---------------:|:------:|:-------:|:-------:|
|CF60012102UJDJ | Vote Name | DELL | Great | UZ062 |
|CF60012102UJDJ | Employee Number | 78562 | Error | UZ062 |
|CF60012102UJDJ | Supplier Number | 222222 | Correct | UZ062 |
The main task is to run through table1 and see any changes to the Result field and update table2 . i have been using EmployeeNIN as a target and source identifier but got error messages as below.
The MERGE statement attempted to UPDATE or DELETE the same row more than once. …
below is my merge statement ..
MERGE [dbo].[Tabel2] AS t
USING (
SELECT * FROM Tabel2
) AS s ON t.EmployeeNIN = s.EmployeeNIN AND t.[KeySection] = s.[KeySection]
WHEN MATCHED THEN
UPDATE SET
t.[Result] = s.[Result]
,t.[Comment] = s.[Comment]
WHEN NOT MATCHED THEN
INSERT (
[EmployeeNIN]
,[KeySection]
,[Result]
,[Comment]
,[Auditor]
)
VALUES (
s.[EmployeeNIN]
,s.[KeySection]
,s.[Result]
,s.[Comment]
,s.[Auditor]
);