Merge multiple tables into one table SQL Server












-1















I want to merge 3 tables into one only to get full data with conditions below




  • merge 3 tables [test1], [test2], [test3] into one table [test]

  • If when merging, if a column is blank select from another table

  • Col1 exists in all 3 tables


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'.











share|improve this question

























  • You probably want to add to the matched part (e.g. when matched and a.col1 = '' or a.col2 = '' ...) assuming you're updating where col1 in test is blank

    – ZLK
    Nov 14 '18 at 3:35













  • Why are updating the main table ? instead of inserting non null values from the 3 tables ?

    – Sanal Sunny
    Nov 14 '18 at 4:01











  • @SanalSunny I think it's the same.

    – Hung Nguyen
    Nov 14 '18 at 5:16











  • @ZLK i think the error syntax is before where clause..

    – Hung Nguyen
    Nov 14 '18 at 5:19











  • The error is because you can't put a where clause there like that. I told you a work-around that is effectively the same. Remove the where clause and add AND ... to the WHEN MATCHED part.

    – ZLK
    Nov 14 '18 at 21:54


















-1















I want to merge 3 tables into one only to get full data with conditions below




  • merge 3 tables [test1], [test2], [test3] into one table [test]

  • If when merging, if a column is blank select from another table

  • Col1 exists in all 3 tables


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'.











share|improve this question

























  • You probably want to add to the matched part (e.g. when matched and a.col1 = '' or a.col2 = '' ...) assuming you're updating where col1 in test is blank

    – ZLK
    Nov 14 '18 at 3:35













  • Why are updating the main table ? instead of inserting non null values from the 3 tables ?

    – Sanal Sunny
    Nov 14 '18 at 4:01











  • @SanalSunny I think it's the same.

    – Hung Nguyen
    Nov 14 '18 at 5:16











  • @ZLK i think the error syntax is before where clause..

    – Hung Nguyen
    Nov 14 '18 at 5:19











  • The error is because you can't put a where clause there like that. I told you a work-around that is effectively the same. Remove the where clause and add AND ... to the WHEN MATCHED part.

    – ZLK
    Nov 14 '18 at 21:54
















-1












-1








-1








I want to merge 3 tables into one only to get full data with conditions below




  • merge 3 tables [test1], [test2], [test3] into one table [test]

  • If when merging, if a column is blank select from another table

  • Col1 exists in all 3 tables


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'.











share|improve this question
















I want to merge 3 tables into one only to get full data with conditions below




  • merge 3 tables [test1], [test2], [test3] into one table [test]

  • If when merging, if a column is blank select from another table

  • Col1 exists in all 3 tables


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'.








sql sql-server tsql sql-server-2008 sql-server-2012






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 5:54







Hung Nguyen

















asked Nov 14 '18 at 3:25









Hung NguyenHung Nguyen

145




145













  • You probably want to add to the matched part (e.g. when matched and a.col1 = '' or a.col2 = '' ...) assuming you're updating where col1 in test is blank

    – ZLK
    Nov 14 '18 at 3:35













  • Why are updating the main table ? instead of inserting non null values from the 3 tables ?

    – Sanal Sunny
    Nov 14 '18 at 4:01











  • @SanalSunny I think it's the same.

    – Hung Nguyen
    Nov 14 '18 at 5:16











  • @ZLK i think the error syntax is before where clause..

    – Hung Nguyen
    Nov 14 '18 at 5:19











  • The error is because you can't put a where clause there like that. I told you a work-around that is effectively the same. Remove the where clause and add AND ... to the WHEN MATCHED part.

    – ZLK
    Nov 14 '18 at 21:54





















  • You probably want to add to the matched part (e.g. when matched and a.col1 = '' or a.col2 = '' ...) assuming you're updating where col1 in test is blank

    – ZLK
    Nov 14 '18 at 3:35













  • Why are updating the main table ? instead of inserting non null values from the 3 tables ?

    – Sanal Sunny
    Nov 14 '18 at 4:01











  • @SanalSunny I think it's the same.

    – Hung Nguyen
    Nov 14 '18 at 5:16











  • @ZLK i think the error syntax is before where clause..

    – Hung Nguyen
    Nov 14 '18 at 5:19











  • The error is because you can't put a where clause there like that. I told you a work-around that is effectively the same. Remove the where clause and add AND ... to the WHEN MATCHED part.

    – ZLK
    Nov 14 '18 at 21:54



















You probably want to add to the matched part (e.g. when matched and a.col1 = '' or a.col2 = '' ...) assuming you're updating where col1 in test is blank

– ZLK
Nov 14 '18 at 3:35







You probably want to add to the matched part (e.g. when matched and a.col1 = '' or a.col2 = '' ...) assuming you're updating where col1 in test is blank

– ZLK
Nov 14 '18 at 3:35















Why are updating the main table ? instead of inserting non null values from the 3 tables ?

– Sanal Sunny
Nov 14 '18 at 4:01





Why are updating the main table ? instead of inserting non null values from the 3 tables ?

– Sanal Sunny
Nov 14 '18 at 4:01













@SanalSunny I think it's the same.

– Hung Nguyen
Nov 14 '18 at 5:16





@SanalSunny I think it's the same.

– Hung Nguyen
Nov 14 '18 at 5:16













@ZLK i think the error syntax is before where clause..

– Hung Nguyen
Nov 14 '18 at 5:19





@ZLK i think the error syntax is before where clause..

– Hung Nguyen
Nov 14 '18 at 5:19













The error is because you can't put a where clause there like that. I told you a work-around that is effectively the same. Remove the where clause and add AND ... to the WHEN MATCHED part.

– ZLK
Nov 14 '18 at 21:54







The error is because you can't put a where clause there like that. I told you a work-around that is effectively the same. Remove the where clause and add AND ... to the WHEN MATCHED part.

– ZLK
Nov 14 '18 at 21:54














2 Answers
2






active

oldest

votes


















1














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.






share|improve this answer
























  • It works when values is NULL, but when value is '' or ' '. How could we use?

    – Hung Nguyen
    Nov 14 '18 at 5:15











  • In that scenario the CASE WHEN statement is to be used. Please replace COALESCE(a.col2, b.col2) with CASE WHEN a.col2 = '' THEN b.col2 END

    – Nimissha
    Nov 15 '18 at 6:17



















0














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






share|improve this answer
























  • I've tried but it does not work with where clause

    – Hung Nguyen
    Nov 14 '18 at 5:15











  • You should check your table structure.

    – fMadTech
    Nov 14 '18 at 5:19











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292747%2fmerge-multiple-tables-into-one-table-sql-server%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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.






share|improve this answer
























  • It works when values is NULL, but when value is '' or ' '. How could we use?

    – Hung Nguyen
    Nov 14 '18 at 5:15











  • In that scenario the CASE WHEN statement is to be used. Please replace COALESCE(a.col2, b.col2) with CASE WHEN a.col2 = '' THEN b.col2 END

    – Nimissha
    Nov 15 '18 at 6:17
















1














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.






share|improve this answer
























  • It works when values is NULL, but when value is '' or ' '. How could we use?

    – Hung Nguyen
    Nov 14 '18 at 5:15











  • In that scenario the CASE WHEN statement is to be used. Please replace COALESCE(a.col2, b.col2) with CASE WHEN a.col2 = '' THEN b.col2 END

    – Nimissha
    Nov 15 '18 at 6:17














1












1








1







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 4:11









NimisshaNimissha

112




112













  • It works when values is NULL, but when value is '' or ' '. How could we use?

    – Hung Nguyen
    Nov 14 '18 at 5:15











  • In that scenario the CASE WHEN statement is to be used. Please replace COALESCE(a.col2, b.col2) with CASE WHEN a.col2 = '' THEN b.col2 END

    – Nimissha
    Nov 15 '18 at 6:17



















  • It works when values is NULL, but when value is '' or ' '. How could we use?

    – Hung Nguyen
    Nov 14 '18 at 5:15











  • In that scenario the CASE WHEN statement is to be used. Please replace COALESCE(a.col2, b.col2) with CASE WHEN a.col2 = '' THEN b.col2 END

    – Nimissha
    Nov 15 '18 at 6:17

















It works when values is NULL, but when value is '' or ' '. How could we use?

– Hung Nguyen
Nov 14 '18 at 5:15





It works when values is NULL, but when value is '' or ' '. How could we use?

– Hung Nguyen
Nov 14 '18 at 5:15













In that scenario the CASE WHEN statement is to be used. Please replace COALESCE(a.col2, b.col2) with CASE WHEN a.col2 = '' THEN b.col2 END

– Nimissha
Nov 15 '18 at 6:17





In that scenario the CASE WHEN statement is to be used. Please replace COALESCE(a.col2, b.col2) with CASE WHEN a.col2 = '' THEN b.col2 END

– Nimissha
Nov 15 '18 at 6:17













0














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






share|improve this answer
























  • I've tried but it does not work with where clause

    – Hung Nguyen
    Nov 14 '18 at 5:15











  • You should check your table structure.

    – fMadTech
    Nov 14 '18 at 5:19
















0














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






share|improve this answer
























  • I've tried but it does not work with where clause

    – Hung Nguyen
    Nov 14 '18 at 5:15











  • You should check your table structure.

    – fMadTech
    Nov 14 '18 at 5:19














0












0








0







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






share|improve this answer













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







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 4:08









fMadTechfMadTech

6912




6912













  • I've tried but it does not work with where clause

    – Hung Nguyen
    Nov 14 '18 at 5:15











  • You should check your table structure.

    – fMadTech
    Nov 14 '18 at 5:19



















  • I've tried but it does not work with where clause

    – Hung Nguyen
    Nov 14 '18 at 5:15











  • You should check your table structure.

    – fMadTech
    Nov 14 '18 at 5:19

















I've tried but it does not work with where clause

– Hung Nguyen
Nov 14 '18 at 5:15





I've tried but it does not work with where clause

– Hung Nguyen
Nov 14 '18 at 5:15













You should check your table structure.

– fMadTech
Nov 14 '18 at 5:19





You should check your table structure.

– fMadTech
Nov 14 '18 at 5:19


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292747%2fmerge-multiple-tables-into-one-table-sql-server%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python