[Solved] How to update an embedded HSQL table from another?

Creating tables and queries
Post Reply
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

[Solved] How to update an embedded HSQL table from another?

Post by Villeroy »

This is how it is documented

Code: Select all

UPDATE table SET column = Expression [, ...] [WHERE Expression];
This is how it should work:

Code: Select all

UPDATE TBLA
SET TXT = TBLB.TXT
FROM TBL2 
WHERE TBLA.ID = TBLB.ID
I get error "Column not found: TBLB.TXT"
Attachments
forum_update.odb
(3.37 KiB) Downloaded 226 times
Last edited by Villeroy on Wed Jul 13, 2022 5:26 pm, edited 1 time in total.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Rafkus_pl
Posts: 7
Joined: Wed Mar 23, 2022 7:29 pm

Re: How to update an embedded HSQL table from another?

Post by Rafkus_pl »

It works like this for me:

Code: Select all

UPDATE TBLA
SET TXT = (SELECT TXT FROM TBLB WHERE TBLA.ID = TBLB.ID )
WHERE EXISTS (SELECT * FROM TBLB WHERE TBLA.ID = TBLB.ID )
Last edited by Rafkus_pl on Wed Jul 13, 2022 7:44 pm, edited 1 time in total.
OpenOffice 4.1.6. and LibreOffice 7.1.6; Widows 10
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: How to update an embedded HSQL table from another?

Post by Villeroy »

Indeed. Thank you very much.
and if there were more columns:

Code: Select all

UPDATE TBLA
SET
  TXT = (SELECT TXT FROM TBLB WHERE TBLA.ID = TBLB.ID ),
  X = (SELECT X FROM TBLB WHERE TBLA.ID = TBLB.ID ),
  Y = (SELECT Y FROM TBLB WHERE TBLA.ID = TBLB.ID ),
  Z = (SELECT Z FROM TBLB WHERE TBLA.ID = TBLB.ID )
WHERE EXISTS (SELECT TXT FROM TBLB WHERE TBLA.ID = TBLB.ID )
as always mind the commas at the ends of some lines.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Rafkus_pl
Posts: 7
Joined: Wed Mar 23, 2022 7:29 pm

Re: [Solved] How to update an embedded HSQL table from another?

Post by Rafkus_pl »

Or you can do it in a slightly roundabout way:
first remove the records from the TBLA table with IDs the same as in the TBLB

Code: Select all

DELETE FROM TBLA 
WHERE ID IN (SELECT ID FROM TBLB)
And then copy the "new" data from the TBLB table to the TBLA

Code: Select all

INSERT INTO TBLA
SELECT * FROM TBLB
Optionally, you can specify one more condition here: add only those records that are not in the TBLA table.

Code: Select all

WHERE NOT ID IN (SELECT ID FROM TBLA)
OpenOffice 4.1.6. and LibreOffice 7.1.6; Widows 10
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Solved] How to update an embedded HSQL table from another?

Post by Villeroy »

Thanks. Your advice is part of [HSQL, Base, Basic] Macro Driven CSV Import
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Post Reply