473,778 Members | 1,852 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to write a single update sql statement to update different setof attributes of a table

How to write a single update sql statement to update different set of
attributes of a table. In other words what will be the value of
indicator variables to ignore updates of some attributes in a update
statement.

e.g. EXEC SQL UPDATE
vA:iA
vB:iB
vC:iC
in TABLE 1 where vD = ...
What should be the indicator values in the above statement if i want to
update only vA and vB but not vC in the database table.

Thanks
galbodada

Jul 19 '05 #1
2 23696
ajay wrote:
How to write a single update sql statement to update different set of
attributes of a table. In other words what will be the value of
indicator variables to ignore updates of some attributes in a update
statement.

e.g. EXEC SQL UPDATE
vA:iA
vB:iB
vC:iC
in TABLE 1 where vD = ...
What should be the indicator values in the above statement if i want to
update only vA and vB but not vC in the database table.

Thanks
galbodada

You can use CASE expression for few columns or UNION ALL for many.
UPDATE T
SET (c1, c2, ... cn)
= (SELECT <expr1>, ... <expri>, ci+1, ..., cn
FROM TABLE(VALUES(1) AS A
WHERE <prd>
UNION ALL
SELECT c1, ..., ci, <expri+1>, ..., <exprn>
FROM TABLE(VALUES 1) AS B
WHERE NOT <prd>)
WHERE ...

Not sure whether TABLE(VALUES ..) is ANSI SQL. You can use whatever
your DBMS supplies to generate a single row table (SYSIBM.SYSDUMM Y1,
DUAL, VALUES(1))

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Jul 19 '05 #2

"ajay" <aj**@speedfact ory.net> wrote in message
news:40******** ******@speedfac tory.net...
How to write a single update sql statement to update different set of
attributes of a table. In other words what will be the value of
indicator variables to ignore updates of some attributes in a update
statement.

e.g. EXEC SQL UPDATE
vA:iA
vB:iB
vC:iC
in TABLE 1 where vD = ...
What should be the indicator values in the above statement if i want to
update only vA and vB but not vC in the database table.

Thanks
galbodada


You can do this with CASE statements.

The general form for this type of update:

EXEC SQL UPDATE <tablename>
SET <colA> CASE WHEN <indA> = 1 THEN <valA> ELSE <colA> END,
<colB> CASE WHEN <indB> = 1 THEN <valB> ELSE <colB> END,
....
<colQ> CASE WHEN <indQ> = 1 THEN <valQ> ELSE <colQ> END
WHERE <colX> = <valX> AND <colY> = <colY>;

And a specific example for a table containing customer information:

EXEC SQL UPDATE customer
SET c_first CASE WHEN :c_first_indica tor = 1 THEN :c_first_value ELSE
c_first END,
c_last CASE WHEN :c_last_indicat or = 1 THEN :c_last_value ELSE c_last END,
c_middle CASE WHEN :c_middle_indic ator = 1 THEN :c_middle_value ELSE
c_middle END
WHERE c_id = :c_id;

If the c_first_indicat or host variable is set to 1, then the c_first column
will be updated to the value of the c_first_value host variable, otherwise
the column is updated with the the current value. Similarly for other
columns.

--
Matt Emmerton
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
11600
by: pb648174 | last post by:
I have a single update statement that updates the same column multiple times in the same update statement. Basically i have a column that looks like .1.2.3.4. which are id references that need to be updated when a group of items is copied. I can successfully do this with cursors, but am experimenting with a way to do it with a single update statement. I have verified that each row being returned to the Update statement (in an...
2
2522
by: serge | last post by:
/* This is a long post. You can paste the whole message in the SQL Query Analyzer. I have a scenario where there are records with values pointing to wrong records and I need to fix them using an Update statement. I have a sample code to reproduce my problem. To simplify the scenario I am trying to use Order related tables to explain a little better the tables i have to work with.
1
1492
by: Stephen Quinney | last post by:
I really cannot tell if this is a bug or I am just doing something stupid. I create a table called wibble: CREATE TABLE wibble (a integer, b integer); I insert some data: INSERT INTO wibble VALUES (1,1);
2
8818
by: ajay | last post by:
How to write a single update sql statement to update different set of attributes of a table. In other words what will be the value of indicator variables to ignore updates of some attributes in a update statement. e.g. EXEC SQL UPDATE vA:iA vB:iB vC:iC in TABLE 1 where vD = ...
1
22885
by: vj | last post by:
How to Update multiple tables in a single SQL update Statement? Is there any way out? vj.
6
2691
by: FayeC | last post by:
I really need help figuring this out. i have a db with mostly text fields but 2. The user_id field is an autonumber (key) and the user_newsletter is a number (1 and 0) field meaning 1 yes the person wants to receive a newsletter and 0 no, don't want to receive it. Now.....when trying to create an UPDATE statement I am running into problems writing the code for it. I had issues before with missing commas but now that the commas are there...
19
8384
by: Steve | last post by:
ASP error number 13 - Type mismatch with SELECT...FOR UPDATE statement I got ASP error number 13 when I use the SELECT...FOR UPDATE statement as below. However, if I use SELECT statement without FOR UPDATE, it is fine and no error. I also tried Set objRs = objConn.Execute("SELECT * FROM EMP UPDATE OF EMPNO"), but it still couldn't help. any ideas? I tried to search in the web but couldn't find similar
8
8200
by: Stephen Plotnick | last post by:
I have three forms and update one table in an Access DB with sucess in one form. In the other two forms I'm trying to do an update to a different table in the Access DB. The error I'm getting in syntax error on Update on the statement da.update(mydataset, "DATABASENAME") I do have a primary key on both and copied the code from the working update
22
5439
by: pbd22 | last post by:
hi. I am having probelms with an update statement. every time i run it, "every" row updates, not just the one(s) intended. so, here is what i have. i have tried this with both AND and OR and neither seem to work. i dont know why this is elluding me, but i'd appreciate help with the solution.
0
9629
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
10127
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10068
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
9923
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...
0
8954
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7474
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
5370
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...
2
3627
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2863
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.