OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Trying to update field from another table using SQL - SOLVED

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Base
View previous topic :: View next topic  
Author Message
flyingfox777
General User
General User


Joined: 18 Mar 2011
Posts: 22

PostPosted: Fri Aug 10, 2012 1:09 am    Post subject: Trying to update field from another table using SQL - SOLVED Reply with quote

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
View user's profile Send private message
Sliderule
Super User
Super User


Joined: 29 May 2004
Posts: 2474
Location: 3rd Rock From The Sun

PostPosted: Fri Aug 10, 2012 10:19 am    Post subject: Reply with quote

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:
  1. From the Menu:

    Tools -> SQL...

  2. 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 Smile .

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
View user's profile Send private message
flyingfox777
General User
General User


Joined: 18 Mar 2011
Posts: 22

PostPosted: Sat Aug 11, 2012 7:26 am    Post subject: Reply with quote

Many thanks Sliderule.javascript:emoticon('Very Happy')
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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Base All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group