Syed Yawar Abbas
Syed Yawar Abbas

Reputation: 11

SQL MERGE: An expression of non-boolean type specified in a context where a condition is expected, near ','

merge into emp_tar.et
    using emp_src.es on (et.emp_id, es.emp_id)
insert into (et.emp_id,et.emp_name)
    values(es.emp_id,es.emp_name)

Whenever I execute this code it gives the error:

An expression of non-boolean type specified in a context where a condition is expected, near ','

Upvotes: 0

Views: 2139

Answers (1)

Pரதீப்
Pரதீப்

Reputation: 93734

Here is the right Merge syntax

Merge into emp_tar et
    using emp_src es on et.emp_id = es.emp_id -- use = to equate two columns
When Not Matched then -- to insert the records only it is not present in target 
insert into (et.emp_id,et.emp_name)
    values(es.emp_id,es.emp_name)

Which is same as

Insert into emp_tar(emp_id,emp_name)
Select es.emp_id,es.emp_name 
From emp_src es 
Where Not exists (select 1 from emp_tar et Where et.emp_id = es.emp_id)

Merge statement can be used when you want to perform more than one DML operation. Also Merge in Sql Server is quite buggy better to use Insert from Select in your case

Use Caution with SQL Server's MERGE Statement

Upvotes: 3

Related Questions