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
6 2889
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.. -
-
-
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)
-
-
Try out this small change...
Hope it helps
Cheers
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.. -
-
-
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)
-
-
Try out this small change...
Hope it helps
Cheers
Hi Hevan,
I didnt understand the problem. Can you be more clear...
Cheers
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,
Hi,
Ok. I get it.
Try this -
-
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),'')
-
WHERE EXISTS (SELECT 1 FROM tab3 c WHERE a.no = c.no fetch first row only)
-
-
-
Hope it works ...
Cheers!!!
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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?
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
| |