473,403 Members | 2,222 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,403 software developers and data experts.

Multiple column updates in a single query

DB2 V8.2 on AIX

I am looking for an efficient way to update several columns in a table
that will have a default change. The problem is the table is large
(million records) and there are 1 to 4 columns that might need to be
changed per record. I wanted to avoid looping through the table 4
times in order to change them.

Here is an example. Basically, the four columns in question were
originally defaulted to NULL. Due to changes elsewhere it can no
longer be NULL so it will be changed to blanks. Here is what the
table would look like prior to changing the default:

table1
col1 bigint
col2 char(8) default null
col3 char(8) default null
col4 char(8) default null
col5 char(8) default null
col6 smallint default 0

So now there are records in the table where some of the values in col2
thru col5 are still NULL and some contain values.

I want to parse the table and change anything that is null in these
four columns to blanks (' '). I am hoping to do with without
having to loop through it four times because I dont know if all
columns are null or maybe only one or two.

Anyone have a good query to do this?

Much Appreciated.

Apr 12 '07 #1
8 11997
Don't know if it's the most efficient way, but you can use a case
expression that returns the blanks when the column is null, else the
column value itself, then use that to unconditionally update all rows.
Something like (not tested):

update table1 set c2=(case when c2 is null then ' ' else c2 end),
c3=(case when c3 is null then ' ' else c3 end), c4=(case when c4 is
null then ' ' else c4 end), c5=(case when c5 is null then ' '
else c5 end)

You might want to chunk up the update if the table is huge and your
active log space is limited. Hope this helps.

Regards,
Miro

On Apr 11, 7:57 pm, "shorti" <lbrya...@juno.comwrote:
DB2 V8.2 on AIX

I am looking for an efficient way to update several columns in a table
that will have a default change. The problem is the table is large
(million records) and there are 1 to 4 columns that might need to be
changed per record. I wanted to avoid looping through the table 4
times in order to change them.

Here is an example. Basically, the four columns in question were
originally defaulted to NULL. Due to changes elsewhere it can no
longer be NULL so it will be changed to blanks. Here is what the
table would look like prior to changing the default:

table1
col1 bigint
col2 char(8) default null
col3 char(8) default null
col4 char(8) default null
col5 char(8) default null
col6 smallint default 0

So now there are records in the table where some of the values in col2
thru col5 are still NULL and some contain values.

I want to parse the table and change anything that is null in these
four columns to blanks (' '). I am hoping to do with without
having to loop through it four times because I dont know if all
columns are null or maybe only one or two.

Anyone have a good query to do this?

Much Appreciated.

Apr 12 '07 #2
shorti wrote:
DB2 V8.2 on AIX

I am looking for an efficient way to update several columns in a table
that will have a default change. The problem is the table is large
(million records) and there are 1 to 4 columns that might need to be
changed per record. I wanted to avoid looping through the table 4
times in order to change them.

Here is an example. Basically, the four columns in question were
originally defaulted to NULL. Due to changes elsewhere it can no
longer be NULL so it will be changed to blanks. Here is what the
table would look like prior to changing the default:

table1
col1 bigint
col2 char(8) default null
col3 char(8) default null
col4 char(8) default null
col5 char(8) default null
col6 smallint default 0

So now there are records in the table where some of the values in col2
thru col5 are still NULL and some contain values.

I want to parse the table and change anything that is null in these
four columns to blanks (' '). I am hoping to do with without
having to loop through it four times because I dont know if all
columns are null or maybe only one or two.

Anyone have a good query to do this?
update T
set (col2, col3, ...) = (coalesce(col2,' '), coalesce(col3, '
'), ...)

/Lennart
Apr 12 '07 #3
On 12 Apr, 00:57, "shorti" <lbrya...@juno.comwrote:
DB2 V8.2 on AIX

I am looking for an efficient way to update several columns in a table
that will have a default change. The problem is the table is large
(million records) and there are 1 to 4 columns that might need to be
changed per record. I wanted to avoid looping through the table 4
times in order to change them.

Here is an example. Basically, the four columns in question were
originally defaulted to NULL. Due to changes elsewhere it can no
longer be NULL so it will be changed to blanks. Here is what the
table would look like prior to changing the default:

table1
col1 bigint
col2 char(8) default null
col3 char(8) default null
col4 char(8) default null
col5 char(8) default null
col6 smallint default 0

So now there are records in the table where some of the values in col2
thru col5 are still NULL and some contain values.

I want to parse the table and change anything that is null in these
four columns to blanks (' '). I am hoping to do with without
having to loop through it four times because I dont know if all
columns are null or maybe only one or two.

Anyone have a good query to do this?

Much Appreciated.
Could you not just do a export (with a case statement for the relevant
4 columns) and then an import from <file>.del of del commitcount 10000
insert_update into <table>.
It might be even quicker, if you were to truncate the table after
you've done the export and then do a load...nonrecoverable.

Apr 12 '07 #4
Don't forget to check the space consequences of changing the columns
from null to blanks. You'll be adding 8-32 bytes for each row containing
null columns. If you don't have enough free space in the existing pages,
rows will physically move during this update. This could have
significant performance consequences for existing SQL.

Phil Sherman
shorti wrote:
DB2 V8.2 on AIX

I am looking for an efficient way to update several columns in a table
that will have a default change. The problem is the table is large
(million records) and there are 1 to 4 columns that might need to be
changed per record. I wanted to avoid looping through the table 4
times in order to change them.

Here is an example. Basically, the four columns in question were
originally defaulted to NULL. Due to changes elsewhere it can no
longer be NULL so it will be changed to blanks. Here is what the
table would look like prior to changing the default:

table1
col1 bigint
col2 char(8) default null
col3 char(8) default null
col4 char(8) default null
col5 char(8) default null
col6 smallint default 0

So now there are records in the table where some of the values in col2
thru col5 are still NULL and some contain values.

I want to parse the table and change anything that is null in these
four columns to blanks (' '). I am hoping to do with without
having to loop through it four times because I dont know if all
columns are null or maybe only one or two.

Anyone have a good query to do this?

Much Appreciated.
Apr 12 '07 #5
On Apr 12, 6:50 am, Phil Sherman <psher...@ameritech.netwrote:
Don't forget to check the space consequences of changing the columns
from null to blanks. You'll be adding 8-32 bytes for each row containing
null columns. If you don't have enough free space in the existing pages,
rows will physically move during this update. This could have
significant performance consequences for existing SQL.

Phil Sherman
Can I just do a reorg after all the records are updated to prevent the
performance problem?

I am leaning towards doing the EXPORT and LOAD of the 4 columns since
I agree this would be the most effecient. I dont expect anyone to be
manipulating the table while this is being done but do I need to
"LOCK" the table? I noticed the LOAD does it's own lock so will this
interfere? I am just concerned with the time between the EXPORT and
the LOAD.

The other queries are great and I will add them to my list for future
use.

(forgive me if this is a duplicate response...my last reply did not
seem to come through after 30+ minutes).

Apr 12 '07 #6
If you will have to reorg after processing the entire table, another
option is to do a SELECT of the entire table, constructing the output as
csv (comma separated variable) data. The SELECT can, using COALESCE,
replace any nulls with blanks. Once the data has been unloaded, change
the table definition from nullable to NOT NULL WITH DEFAULT and use the
load utility to rebuild the table. This will, however involve a lot more
DBA work. It'll also allow existing code to continue running and have
new data rows have appropriate defaults.

Phil Sherman
shorti wrote:
On Apr 12, 6:50 am, Phil Sherman <psher...@ameritech.netwrote:
>Don't forget to check the space consequences of changing the columns
from null to blanks. You'll be adding 8-32 bytes for each row containing
null columns. If you don't have enough free space in the existing pages,
rows will physically move during this update. This could have
significant performance consequences for existing SQL.

Phil Sherman

Can I just do a reorg after all the records are updated to prevent the
performance problem?

I am leaning towards doing the EXPORT and LOAD of the 4 columns since
I agree this would be the most effecient. I dont expect anyone to be
manipulating the table while this is being done but do I need to
"LOCK" the table? I noticed the LOAD does it's own lock so will this
interfere? I am just concerned with the time between the EXPORT and
the LOAD.

The other queries are great and I will add them to my list for future
use.

(forgive me if this is a duplicate response...my last reply did not
seem to come through after 30+ minutes).
Apr 13 '07 #7
On Apr 13, 8:33 am, Phil Sherman <psher...@ameritech.netwrote:
If you will have to reorg after processing the entire table, another
option is to do a SELECT of the entire table, constructing the output as
csv (comma separated variable) data.
You want to be VERY careful using csv files; most especially you want
to make sure there aren't any commas in your data, or you will not be
able to reload the data correctly. Also, I'm not sure what non-US
ASCII data would do - if your data is like that, you need to read the
documentation very carefully to make sure you know the limitations of
the csv (called DEL format) loading utility.

I'd be more inclined to recomment the ASC format (fixed width) or IXF
(internal) - the first requires quite a bit more setup, though.
However, both of these should successfully reload all of the data
correctly - no matter what the data is.

-Chris

Apr 16 '07 #8
Ian
ChrisC wrote:
On Apr 13, 8:33 am, Phil Sherman <psher...@ameritech.netwrote:
>If you will have to reorg after processing the entire table, another
option is to do a SELECT of the entire table, constructing the output as
csv (comma separated variable) data.

You want to be VERY careful using csv files; most especially you want
to make sure there aren't any commas in your data, or you will not be
able to reload the data correctly. Also, I'm not sure what non-US
ASCII data would do - if your data is like that, you need to read the
documentation very carefully to make sure you know the limitations of
the csv (called DEL format) loading utility.
DB2 is very good with its delimited format.

By default the DEL format uses both character AND column delimiters.
The default character delimiter is a double-quote ("), and the
default column delimiter is a comma (,).

DB2 will handle column delimiters as long as the character delimiters
exist.

It will even handle embedded newlines (provided you use the
delprioritychar filetype-modifier).
I'd be more inclined to recomment the ASC format (fixed width) or IXF
(internal) - the first requires quite a bit more setup, though.
However, both of these should successfully reload all of the data
correctly - no matter what the data is.
You can't export data in fixed-width format from DB2 (on
Linux/Unix/Windows)

Apr 18 '07 #9

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

Similar topics

6
by: Steven An | last post by:
Howdy, I need to write an update query with multiple aggregate functions. Here is an example: UPDATE t SET t.a = ( select avg(f.q) from dbo.foo f where f.p = t.y ), t.b = ( select sum(f.q)...
1
by: Gregory.Spencer | last post by:
Hi there, Using PHPMyAdmin and it is very usefully reporting problems with my MySQL DB. "PRIMARY and INDEX keys should not both be set for column `column_name`" and
8
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...
5
by: rdemyan via AccessMonster.com | last post by:
I have a need to add another field to all of my tables (over 150). Not data, but an actual field. Can I code this somehow. So the code presumabley would loop through all the tables, open each...
7
by: =?Utf-8?B?QVRT?= | last post by:
HOWTO Run multiple SQL statements from ASP/ADO to an Oracle 10g. Please help, I'm trying to write an ASP page to use ADO to run a long query against an Oracle 10g database, to create tables,...
7
by: =?Utf-8?B?TG9zdEluTUQ=?= | last post by:
Hi All :) I'm converting VB6 using True DBGrid Pro 8.0 to VB2005 using DataGridView. True DBGrid has a MultipleLines property that controls whether individual records span multiple lines. Is...
25
by: Darsin | last post by:
Hi all I need to perform a summation on a column of a table based on a criteria given in another column in the same table. The catch is i need to perform different sums according to the number of...
1
by: ll | last post by:
Hi all, I've inherited a site and am currently looking to redesign a page that displays a table of course curriculum, with each row representing a "Topic" of the curriculum. There is a courseID...
8
by: modernshoggoth | last post by:
G'day all, thanks in advance for reading. I've got two tables, one that's full of data regularly externally updated called "tblEmployeeLicences": EmployeeID,Name,Licences 1001,Bill,Drivers...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
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...
0
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...

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.