If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Multi-Row-Update

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-01-09, 11:19
olel olel is offline
Registered User
 
Join Date: Oct 2009
Posts: 23
Multi-Row-Update

Hi,

I need some help with a somehow weird update statement. I have three tables Item, Bill and Item_Bill. Item and Bill have a n:m relationship in our domain model so that we have the connection table Item_Bill which has three attributes: item_id, bill_id and some_id (see below).

In the table Item we have an attribute "some_id" that is mandatory and whose function isn't relevant for this problem. In the table Item_Bill we also have this attribute "some_id" but it is optional here and actually always empty.

Is it possible to write a sql update statement to fill the "some_id" attributes in all Item_Bill entries with the corresponding "some_id" from Item? I should look like:

Code:
update Item_Bill set some_id = (select some_id from Item where Item.id = Item_Bill.item_id)
and it would be nice if it updates all Item_Bill entries at once.

Thanks,
Ole
Reply With Quote
  #2 (permalink)  
Old 12-01-09, 11:42
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,002
What DBMS are you using? ANSI update behaviour/syntaxt differs from, say, Microsoft SQL Server, so yo may get a more appropriate answer if you supply us with this information
__________________
George
Twitter | Blog
Reply With Quote
  #3 (permalink)  
Old 12-01-09, 12:17
olel olel is offline
Registered User
 
Join Date: Oct 2009
Posts: 23
Our customer is using DB2. Nevertheless it would be great to have an statement which will work on any sql database as we are using Hibernate as our ORM and would like to be independent from our customers DBMS.
Reply With Quote
  #4 (permalink)  
Old 12-02-09, 04:05
JarlH JarlH is offline
Registered User
 
Join Date: Dec 2008
Location: At work...
Posts: 58
Quote:
Originally Posted by olel View Post
Nevertheless it would be great to have an statement which will work on any sql database [...] and would like to be independent from our customers DBMS.
Your example above is ANSI/ISO SQL compliant. That searched update syntax has been in the SQL standard for ages, at least since SQL-92, so I suppose most DBMS products support it.
Reply With Quote
  #5 (permalink)  
Old 12-03-09, 03:34
olel olel is offline
Registered User
 
Join Date: Oct 2009
Posts: 23
Hmm, funny thing. I thought I had tried my statement some days ago and it had not worked, but another try today has proved me wrong.
Reply With Quote
  #6 (permalink)  
Old 12-04-09, 11:08
scooby_at_work scooby_at_work is offline
Registered User
 
Join Date: Sep 2009
Posts: 44
Quote:
Originally Posted by olel View Post
Nevertheless it would be great to have an statement which will work on any sql database...
Yes, we can dream, can't we.
Reply With Quote
  #7 (permalink)  
Old 12-05-09, 12:36
dportas dportas is offline
Registered User
 
Join Date: Dec 2007
Location: London, UK
Posts: 732
You can use the MERGE statement to do an update between two tables based on a join. MERGE is ISO standard SQL and is supported by Oracle, MS SQL Server and DB2 among others.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On