Search code examples
mysqlmariadbmariasql

MARIADB : Insert and Update table based on data from another table


/* Here is the code i used to merge but i get error and am unable update Error : SQL Error [1064][42000] You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the righr syntax to use near 'Merge temp2 as t' at line 1 */

BEGIN
MERGE temp2 as t
using temp1 as s ON (t.slno = s.slno)
-- Insert values when data no present
WHEN NOT MATCHED THEN INSERT VALUES
(s.slno,s.name,s.address);
-- Update when values present
WHEN MATCHED then UPDATE SET
t.slno = s.slno,
t.name = s.name,
t.address = s.address;
END

Solution

  • You could probably use this:

    INSERT INTO temp2 (slno, name, address) SELECT slno, name, address FROM temp1 
    ON DUPLICATE KEY UPDATE 
    slno = VALUES(slno), name = VALUES(name), address = VALUES(address)