DB2 Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDB2 Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old June 22nd, 2007, 09:31 AM
dotnetdeveloper dotnetdeveloper is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 3 dotnetdeveloper User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 7 m 56 sec
Reputation Power: 0
Update one table with data from a 2nd table

Hello,

I'm pretty new to DB2 SQL programming, and I've been banging my head for 2 weeks now trying to figure out the following situation. Here is an example of what i'm trying to do:

I have the 2 tables (Orders and Temp_Orders):

-------- --------------
ORDERS TEMP_ORDERS
-------- --------------
StoreID OID Order_Name StoreID OID Order_Name
------- --- ------------ ------- --- ------------
777 111 -- 777 111 Order1
888 111 -- 888 111 Order2
999 111 -- 999 111 Order2
222 516 -- 222 727 Order9

So i'm trying to update all of the values in the base table from this temp table, but only for the items with a OID of 111. This is the query that i'm trying to use (but it always fails since I have more than one row coming back from the temp table):

UPDATE Orders
SET Order_Name = ( SELECT Order_Name FROM Temp_Orders WHERE OID = 111)
WHERE OID = 111;

The subquery returns the data that I need to transfer over, but since its more than one row, the update query always fails for me. Does anyone know of a better way to do this? Oh, and I'm using DB2 version 7. Any help would be greatly appreciated.

Thanks!

Reply With Quote
  #2  
Old June 23rd, 2007, 10:19 AM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111
As far as I know, you have a couple of options.

1) Either delete all rows in TEMP_ORDERS with the OID of 111, and then insert them using this:

DELETE from TEMP_ORDERS where OID = 111;
INSERT INTO TEMP_ORDERS (StoreID, OID, Oder_Name) select * from ORDERS where OID = 111;

2) in whatever programming language you are using, select all rows from ORDERS with oid-111, loop over them, and for each row, update temp_orders.order_name where orders.oid = temp_orders.oid and orders.store_id = temp_orders.store_id
__________________
~Nemi

Before posting did you try:
[ Javadocs | Google ]

Reply With Quote
  #3  
Old June 25th, 2007, 09:34 AM
dotnetdeveloper dotnetdeveloper is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 3 dotnetdeveloper User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 7 m 56 sec
Reputation Power: 0
First, Thank you very much for your reply.

However, I see after reading your reply that I left off a little bit of important information. These tables have referential integrity to other tables, so if I would not be allowed to delete the rows in the Orders table (in order to copy in new data from temp_orders) because of the rows of data in the other associated tables. So I need to be able to perform an update to the orders table, and not a delete and insert as you suggested.

Do you see any other potential solutions to this issue?

Thank You.

Reply With Quote
  #4  
Old June 25th, 2007, 10:07 AM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111
The second solution should work for you. Are you using .NET? Select all the rows from ORDERS where OID=111. Then loop through the result set and for each row, update the corresponding row in TEMP_ORDERS where the OID and store_id match. You're really only changing the order_name, right?

Reply With Quote
  #5  
Old June 27th, 2007, 09:37 AM
dotnetdeveloper dotnetdeveloper is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 3 dotnetdeveloper User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 7 m 56 sec
Reputation Power: 0
Yes, I'm using .Net. I will try to see if I can implement this from the .Net code (without using embedded SQL). My only concern is the fact that this update process has to be transactional since there are 2 other processes that must occur. There is one insert into some other table that occurs before the UPdate of the Orders table, and a delete process which must occur after this update of the Orders table. These 2 other processes are already implemented in a single stored proc which functions correctly. I was just trying to add this update process in the middle to the same stored proc so that keeping this entire process transactional would be a little easier.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDB2 Development > Update one table with data from a 2nd table


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway