473,394 Members | 1,717 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Help: UPDATE FIELD WHERE CONDITION BASED ON SELECT QUERY FROM MULTIPLE TABLES.

Hello,

I am struggling to build a query on a DB2 db wich does an update for multiple fields which are the result of a select query with where clause (using multiple tables in the condition).

E.g.
UPDATE TABLE_HDR
SET HDRFIELD1 = 'XX'
WHERE EXIST
(SELECT HDRFIELD1 FROM TABLE_HDR,TABLE_1A,TABLE_1B WHERE FIELD_1A= FIELD_1B AND HDRFIELD1=FIELD1A)

I always get the same result where it ignores my select query and updates HDRFIELD1 for all rows.

What I basically want is that it only updates HDRFIELD1 for the ROWS returned in the SELECT statement.

e.g. SELECT produces 25 rows then it should only update HDRFIELD1='XX' for those 25 rows.

Any ideas?
MARIEDB2
Sep 2 '08 #1
5 11002
the reason it is updating all the rows is that because you have mentioned EXISTS in the wehere clause. So even if one rows exists, it will update all rows. You may want to try:

UPDATE TABLE_HDR
SET HDRFIELD1 = 'XX'
WHERE HDRFIELD1 IN (SELECT HDRFIELD1 FROM TABLE_HDR,TABLE_1A,TABLE_1B WHERE FIELD_1A= FIELD_1B AND HDRFIELD1=FIELD1A)

this is not performance tuned at all.
Sep 3 '08 #2
Hi,

Thanks for your reply.

When I try to run the query as per your above example I get an error SQL0412 "Subquery with more then one result column not valid."

UPDATE TABLE_HDR
SET HDRFIELD1 = 'XX'
WHERE HDRFIELD1 IN (SELECT HDRFIELD1 FROM TABLE_HDR,TABLE_1A,TABLE_1B WHERE FIELD_1A= FIELD_1B AND HDRFIELD1=FIELD1A)

(result set of the select is 21 rows for which I want to update HDRFIELD1='a_value')

Also you mentioned this is not performance tuned. This concerns me. How can it run more efficiently?

Any other suggestions that might work?

Thank you so much!
Sep 3 '08 #3
Sorry I need to correct something in my previous post!

Due to my select statement containing multiple columns it was giving me the error.
I have retested by using the select statement, selecting only that column. After that it ran but it updated all fields in the table rather then just the fields for which we ran a select.

Back to square one -((

Could it be at all related to the fact I am running it with razor?
Sep 3 '08 #4
r035198x
13,262 8TB
..
I have retested by using the select statement, selecting only that column. After that it ran but it updated all fields in the table rather then just the fields for which we ran a select.
...
Shouldn't do that. Let's see the code that you ran this time.
Sep 4 '08 #5
Shouldn't do that. Let's see the code that you ran this time.

Thank you all for your reply the previous sql statement worked!

Many thanks for your responses!
Sep 9 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Lauren Quantrell | last post by:
In VBA, I constructed the following to update all records in tblmyTable with each records in tblmyTableTEMP having the same UniqueID: UPDATE tblMyTable RIGHT JOIN tblMyTableTEMP ON...
28
by: stu_gots | last post by:
I have been losing sleep over this puzzle, and I'm convinced my train of thought is heading in the wrong direction. It is difficult to explain my circumstances, so I will present an identical...
3
by: pw | last post by:
Hi, I am having a mental block trying to figure out how to code this. Two tables: "tblQuestions" (fields = quesnum, questype, question) "tblAnswers" (fields = clientnum, quesnum, questype,...
8
by: newbie | last post by:
hello How can I update more than one tables on the same form? The relationship is one to many from a master table to 3 other related tables. on the form, I have to setvalue for a few...
11
by: my-wings | last post by:
I think I've painted myself into a corner, and I'm hoping someone can help me out. I have a table of books (tblBooks), which includes a field (strPubName) for Publisher Name and another field...
15
by: Jay | last post by:
I have a multi threaded VB.NET application (4 threads) that I use to send text messages to many, many employees via system.timer at a 5 second interval. Basically, I look in a SQL table (queue) to...
0
by: Chuck36963 | last post by:
Hi all, I've been working on a listing problem and I can't figure out how to work it out. I have looked far and wide on the web to find answers, but I'd like other peoples input on my project in...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
11
by: troy_lee | last post by:
I have two fields on a form. These two fields' values are based on an expression and represent a date range. I need to create a SQL statement that will use the returned values of these two fields...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.