I want to merge 3 tables into one only to get full data with conditions below
Here is my example code to merge from table 2 into test.
MERGE INTO [dbo].[test] a
USING [dbo].[test2] b ON a.col1 = b.col1
WHEN MATCHED THEN
UPDATE
SET col1 = b.col1,
col2 = b.col2,
col3 = b.col3,
col4 = b.col4
WHERE col1 = '' OR col2 = '' OR col3 = '' OR col4 = '';
It get errors:
Incorrect syntax near the keyword 'where'.
Please try the COALESCE function rather than using the where condition. The COALESCE function takes the next value in case there is a NULL is encountered.
Merge into [dbo].[test] a
using [dbo].[test2] b
on a.col1 = b.col1
when matched then
update
set col2 = COALESCE(a.col2, b.col2),
col3 = COALESCE(a.col3, b.col3),
col4 = COALESCE(a.col4, b.col4);
So in the modified code, if the Table [dbo].[test] col2 is Null then it takes the value from [dbo].[test2]. Since you will be using COALESCE, you will be able to merge any number of values.
Can you show the table Structure.
you can use UNION ALL
Operator follow this link.
https://www.techonthenet.com/sql_server/union.php
OR mixture of INNER JOIN
with UPDATE
statement combining the condition on WHERE
clause
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.