Reputation: 213
I have to update a table column with inner join with other table.I have tried using the below sql.But i'm getting error in Databricks as (Error in SQL statement: ParseException: mismatched input '' expecting 'WHEN').I tried different ways of updating the table.Can someone help me on this issue how to fix this?
%sql
merge into test a using test_1 b
on (a.id_num=b.id_num)
when matched then
update set a.name=b.name;
Upvotes: 3
Views: 27537
Reputation: 1026
If I am getting your question correct you want to use databricks merge into construct to update your table 1 (say destination) columns by joining it to other table 2( source)
MERGE INTO destination
USING updates
ON destination.primary_key = source.foregin_key
WHEN MATCHED THEN
UPDATE SET column1= updates.column1
Same query can be extended to insert data as well if no marching row is present in source and destination table.
Upvotes: 1
Reputation: 41
%%sql
merge into test a using test_1 b
on (a.id_num=b.id_num)
when matched then
update set a.name=b.name;
use double % for this.
Upvotes: 4
Reputation: 4179
query which you are using is correct. Reason for failure is you are not using delta tables, in your case test and test_1 should be delta tables.
Upvotes: 0
Reputation: 397
This is because of Databricks Runtime Version, You need to use the Databricks Runtime version 5.1
or above for your query on Delta Table. But for the merge operation, you need to have a Delta table first.
Upvotes: 0
Reputation: 527
Your table must be DELTA format. is it?
I think your command is ok.
https://docs.databricks.com/spark/latest/spark-sql/language-manual/merge-into.html
Upvotes: 1