| View previous topic :: View next topic |
| Author |
Message |
flyingfox777 General User

Joined: 18 Mar 2011 Posts: 22
|
Posted: Fri Aug 10, 2012 1:09 am Post subject: Trying to update field from another table using SQL - SOLVED |
|
|
Hi,
Simple problem but not working.
Working with Base as the frontend.
I have two tables (A&B) in the same database which are linked by field "CTP_ID".
CTP_ID in table A is the primary key, in table B it is an integer.
Table B has a separate field, ID, as the primary key.
Both tables contain a field C.
(For info Table B is an older version of Table A but with the addition of the ID field and CT_ID changed to an integer).
I wish to write an SQL expression that updates field C values in table A with those that are in table B. (I can display the two fields using a simple query from the query builder.)
Query used:-
UPDATE "A" set "A"."C"="B"."C" WHERE EXISTS (SELECT 1 FROM "A" WHERE "A"."CTP_ID" = "B"."CTP_ID" )
All attempts to-date fail :- user lacks privilege or object not found: B.CTP_ID.
Any pointers would be very much appreciated. _________________ Always in the proverbial - only the depth varies.
Running LibreOffice 3.5.4.2, HSQLDB 2.2.9 with JDBC (separate database from frontend) on Ubuntu 12.04 X86-64 all on one machine.
Last edited by flyingfox777 on Sat Aug 11, 2012 7:24 am; edited 1 time in total |
|
| Back to top |
|
 |
Sliderule Super User


Joined: 29 May 2004 Posts: 2474 Location: 3rd Rock From The Sun
|
Posted: Fri Aug 10, 2012 10:19 am Post subject: |
|
|
You should be able to use the following SQL ( Structured Query Language ) to make the modifications you described.
It is important to note, that, this SQL Code can only be executed, EITHER:
- From the Menu:
Tools -> SQL...
- From within a Macro
| Code: | UPDATE "A" SET "A"."C" =
(SELECT "B"."C" FROM "B" WHERE "B"."CTP_ID" = "A"."CTP_ID")
WHERE EXISTS (Select "B"."C" From "B" Where "B"."CTP_ID" = "A"."CTP_ID") |
Explanation: In the above, it is assigning the value of "A"."C" to the value of "B"."C", but, only for the records that match the WHERE EXISTS condition .
I hope this helps, please be sure to let me / us know.
Sliderule
Thanks to add [Solved] in your first post Title ( edit button ) if your issue has been fixed / resolved. |
|
| Back to top |
|
 |
flyingfox777 General User

Joined: 18 Mar 2011 Posts: 22
|
Posted: Sat Aug 11, 2012 7:26 am Post subject: |
|
|
Many thanks Sliderule.javascript:emoticon(' ')
Solution worked a treat. _________________ Always in the proverbial - only the depth varies.
Running LibreOffice 3.5.4.2, HSQLDB 2.2.9 with JDBC (separate database from frontend) on Ubuntu 12.04 X86-64 all on one machine. |
|
| Back to top |
|
 |
|