Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Syntax error on table update

Why is that I'm getting a syntax error on the following SQL statement under sqlite?

Error reads:

SQL Error: near "SET": syntax error

UPDATE nova
       SET Nome = (select Nome from assessores where nova.ID = assessores.ID),
       SET Morada = (select Morada from assessores where nova.ID = assessores.ID),
       SET Email = (select Email from assessores where nova.ID = assessores.ID),
       SET TelfCasa = (select TelfCasa from assessores where nova.ID = assessores.ID),
       SET TelfEmprego = (select TelfEmprego from assessores where nova.ID = assessores.ID),
       SET Telemovel = (select Telemovel from assessores where nova.ID = assessores.ID),
       SET Fax = (select Fax from assessores where nova.ID = assessores.ID)
WHERE EXISTS (select * from assessores where nova.ID = assessores.ID);

If I try to fully qualify the SET field names, the error becomes:

SQL Error: near ".": syntax error

like image 647
Alexandre Bell Avatar asked May 10 '26 23:05

Alexandre Bell


1 Answers

You only need one SET at the beginning. You can also simplify the query by joining the two tables together and eliminating the sub-queries.

UPDATE nova JOIN assessores ON nova.ID = assessores.ID
SET nova.Nome        = assessores.Nome,
    nova.Morada      = assessores.Morada,
    nova.Email       = assessores.Email,
    nova.TelfCasa    = assessores.TelfCasa,
    nova.TelfEmprego = assessores.TelfEmprego,
    nova.Telemovel   = assessores.Telemovel,
    nova.Fax         = assessores.Fax;
like image 119
John Kugelman Avatar answered May 14 '26 09:05

John Kugelman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!