473,387 Members | 1,569 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,387 software developers and data experts.

Updating a large table

17
Hi,

I am using this sql for updating a large table.

This sql should update a record like 'abc123'. The first select will return 'abc' and the second select will return '123'.
This works fine if the second select is not null.
If the second select returns null then the updated record should be a.p_cd = 'abc'. But it shows null.

If i use two seperate updates It works fine. But i want to use one update
query:

update tab1 a set a.p_cd = (select b.p_cd from tab2 b where b.type = a.type fetch first row only)
|| (select c.p_type from tab3 c where a.no = c.no fetch first row only)
where exists (select 1 from tab3 c where a.no = c.no fetch first row only)

Please let me know if you have any ideas.

Thanks,
Hevan
May 25 '07 #1
6 2889
frozenmist
179 Expert 100+
Hi Hevan,
This is because when a string is concatenated with a null value in DB2, the result will be NULL.
you can use value to solve it..

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. update tab1 a set  a.p_cd = (select value(b.p_cd,'' ) from tab2 b where b.type = a.type fetch first row only)
  4.  ||   (select value(c.p_type,'' ) from tab3 c where a.no = c.no fetch first row only) 
  5. where   exists (select 1 from tab3 c where  a.no = c.no fetch first row only) 
  6.  
  7.  
Try out this small change...

Hope it helps
Cheers
May 26 '07 #2
Hevan
17
Hi Frozenmist,

Thankyou for your help. Still, It updates spaces if the second select is null.

Instead of abc(with spaces)

Thanks,
Hevan















Hi Hevan,
This is because when a string is concatenated with a null value in DB2, the result will be NULL.
you can use value to solve it..

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. update tab1 a set  a.p_cd = (select value(b.p_cd,'' ) from tab2 b where b.type = a.type fetch first row only)
  4.  ||   (select value(c.p_type,'' ) from tab3 c where a.no = c.no fetch first row only) 
  5. where   exists (select 1 from tab3 c where  a.no = c.no fetch first row only) 
  6.  
  7.  
Try out this small change...

Hope it helps
Cheers
May 29 '07 #3
frozenmist
179 Expert 100+
Hi Hevan,
I didnt understand the problem. Can you be more clear...

Cheers
May 30 '07 #4
Hevan
17
Hi,

I have 3 tables which i have to update tab1 using this update.

tab1 (no integer, type varchar(5), p_cd varchar(10))

values
10, abc1
10, abc1
20, aaa1
20, aaa1
30, bbb1

tab2(p_cd(5), type(5)
values
abc, abc1
aaa, aaa1
bbb, bbb1
cc, ccc2

tab3(no integer, p_type varchar(5)
values
10, 123
10, 123
20, 222
20, 222

UPDATE tab1 a SET a.p_cd = (SELECT value(b.p_cd,'' ) FROM tab2 b WHERE b.type = a.type fetch first row only)
|| (SELECT value(c.p_type,'' ) FROM tab3 c WHERE a.no = c.no fetch first row only)
WHERE EXISTS (SELECT 1 FROM tab3 c WHERE a.no = c.no fetch first row only)

if i run the update sql i should get 10, abc1, abc123
10, abc1, abc123
20, aaa1, aaa222
20, aaa1, aaa222
30, bbb1, bbb


instead of this i get 10, abc1, abc123
10, abc1, abc123
20, aaa1, aaa222
20, aaa1, aaa222
30, bbb1,
May 30 '07 #5
frozenmist
179 Expert 100+
Hi,
Ok. I get it.
Try this

Expand|Select|Wrap|Line Numbers
  1.  
  2. UPDATE tab1 a SET  a.p_cd = value((SELECT b.p_cd FROM tab2 b WHERE b.type = a.type fetch first row only),'')
  3. ||   value((SELECT c.p_type FROM tab3 c WHERE a.no = c.no fetch first row only),'')
  4.     WHERE   EXISTS (SELECT 1 FROM tab3 c WHERE  a.no = c.no fetch first row only)
  5.  
  6.  
  7.  

Hope it works ...
Cheers!!!
May 31 '07 #6
Hevan
17
Hi,

UPDATE tab1 a SET a.p_cd = value((SELECT b.p_cd FROM tab2 b WHERE b.type = a.type fetch first row only),'')
|| value((SELECT c.p_type FROM tab3 c WHERE a.no = c.no fetch first row only),'')


It worked . Great!

Thankyou,
Hevan
May 31 '07 #7

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

Similar topics

11
by: Jason | last post by:
Let's say I have an html form with 20 or 30 fields in it. The form submits the fields via POST to a php page which updates a table in a database with the $_POST vars. Which makes more sense? ...
13
by: omission9 | last post by:
I have a dictionary that looks like this MY_DICT=FOO I am having a problem updating this with a simple MY_DICT.update(NEW_DICT) as update doesn't seem to care about getting into the inner...
1
by: Roy Adams | last post by:
Hi everyone I'm trying to build a shopping cart app using a db the part I'm stuck on is the fact that, if someone adds a product that they have previously added to the cart. I've got it set up to...
3
by: teddysnips | last post by:
This from a SQL Server manual: "Complex queries, however, such as those in decision support systems, can reference large numbers of rows in base tables and aggregate large amounts of information...
1
by: Srinadh | last post by:
Hi all, We have files with about 20 to 30 fields per row. We are trying to update such files with about 60 rows as contiguous data in a CLOB field. It passes through. But when we try...
3
by: Tc | last post by:
Hi, I was curious, I am thinking of writing an application that loads a dataset from a database that resides on a server. The question I have is this, if multiple copies of the app will be...
0
by: Pavel Sorokin | last post by:
Hello, I created a table where a record consists of a single field of type lo. I'm trying to add a new record and store the contents of a file in it. However, when I call CRecordset.Update() I...
33
by: bill | last post by:
In an application I am writing the user can define a series of steps to be followed. I save them in a sql database using the field "order" (a smallint) as the primary key. (there are in the range...
3
by: HSXWillH | last post by:
I've looked through the site and not found what I'm looking for here. I am not code-versed or anything like that so my skills are rudimentary at best. I'm using Access 03 on a Windows Vista...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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
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
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...

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.