473,626 Members | 3,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

4 New Member
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=FIELD 1A)

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 11043
spider007
27 New Member
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=FIELD 1A)

this is not performance tuned at all.
Sep 3 '08 #2
MARIEDB2
4 New Member
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=FIELD 1A)

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

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
MARIEDB2
4 New Member
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 MVP
..
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
MARIEDB2
4 New Member
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
89304
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 tblMyTable.UniqueID = tblMyTableTEMP.UniqueID SET tblMyTable.myField = tblMyTableTEMP.myField, tblMyTable.myField2 = tblMyTableTEMP.myField2,
28
3293
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 make-believe challenge in order to avoid confusing the issue further. Suppose I was hosting a dinner and I wanted to invite exactly 12 guests from my neighborhood. I'm really picky about that... I have 12 chairs besides my own, and I want them all...
3
1857
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, answer) They are related by quesnum and questype. There are records in
8
1981
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 fields(colums) in the related tables based on the value I enter in the field from the master table. I kept getting "you have to save the record before you update..." kind of message. Do you have any idea how to handle this? I feel I have to set the master...
11
2793
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 (strPubCity) for Publisher City. These two fields have a many-to-one relationship with tables, (tlkpPubName and tlkpPubCity) respectively. The lookup tables only have one field (strPubName and strPubCity), which is their primary key. I also have...
15
2569
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 determine who needs to receive the text message then send the message to the address. Only problem is, the employee may receive up to 4 of the same messages because each thread gets the recors then sends the message. I need somehow to prevent...
0
2439
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 the whole. I really need MySQL wizz to give me a hand (and maybe refer me to books to get me to the wizz level myself). First off, english is a second language to me and sometimes my sentences might be a little awkward. Please forgive me. Mon...
0
5557
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 ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
11
319
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 in the WHERE clause as a date range (less than or equal to, and greater than or equal to the upper and lower dates of the range). I then need to count the total number of records returned from the query and output the record count to another...
0
8262
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8701
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8364
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6122
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4090
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4196
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2623
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1507
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.