View previous topic :: View next topic |
Author |
Message |
psridhar Beginner
Joined: 16 May 2004 Posts: 68 Topics: 26
|
Posted: Fri Jun 25, 2004 12:06 pm Post subject: Update with Not Null |
|
|
Hi,
I have a small problrm with the updation. I have two columns in a table T1 and two columns in table T2. For me master table is T1.
Columns are like this
Table 1
--------
Code: |
OrderNo OrderName
--------- -------------
1 XXX
2 YYY
3 ZZZ
4 AAA
.........................
........................
|
Table 2
---------
Code: |
OrderNo OrderName
--------- -------------
1
1
2
2
3
2
3
4
5
|
OrderName in table T2 should be updated from table T1. OrderName should not contain null values. If OrderName is not found in Master table T1 then OrderName in T2 should be say PPP a unique value which will not be there in T1.
For this requirement, I tried the following SQL.
Code: |
UPDATE T2
SET T2.OrderType = (
SELECT IFNULL(T1.OrderType,'XXX')
FROM T1
WHERE T1.OrderNo = T2.OrderNo ) ;
|
I got an error message
DSNT408I SQLCODE = -407, ERROR: AN UPDATE, INSERT, OR SET VALUE IS NULL, BUT THE OBJECT COLUMN CANNOT CONTAIN NULL VALUES DSNT418I SQLSTATE = 23502 SQLSTATE RETURN CODE
Can anybody help me out .......
Sri |
|
Back to top |
|
 |
SureshKumar Intermediate
Joined: 23 Jan 2003 Posts: 211 Topics: 21
|
Posted: Fri Jun 25, 2004 12:31 pm Post subject: |
|
|
Sri,
Can you be clear about what is needed, you are trying to update a different column 'ordertype' than what you have refered to. So there is a third column in play - is ordertype nullable ? |
|
Back to top |
|
 |
psridhar Beginner
Joined: 16 May 2004 Posts: 68 Topics: 26
|
Posted: Fri Jun 25, 2004 12:43 pm Post subject: |
|
|
I am sorry...... It is OrderName |
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
Posted: Fri Jun 25, 2004 12:43 pm Post subject: |
|
|
Sridhar,
What is your DB2 Version?
I don't think you can update the table with 1 sql statement.Try this
Code: |
UPDATE T2
SET ORDER_NAME = (SELECT ORDER_NAME FROM T1
WHERE T1.ORDER_NO = T2.ORDER_NO)
;
UPDATE T2
SET ORDER_NAME = 'PPP'
WHERE ORDER_NO NOT IN (SELECT ORDER_NO FROM T1)
;
|
This will produce
Code: |
ORDER_NO ORDER_NAME
------- ----------
1 XXX
1 XXX
1 XXX
2 YYY
2 YYY
3 ZZZ
2 YYY
3 ZZZ
4 AAA
5 PPP
|
Hope this helps...
Cheers
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
 |
psridhar Beginner
Joined: 16 May 2004 Posts: 68 Topics: 26
|
Posted: Fri Jun 25, 2004 12:46 pm Post subject: |
|
|
Hi Kolusu,
I am using DB2 V7.
Thanks alot.....
Can you help me in understanding what is IFNULL and when it is used? |
|
Back to top |
|
 |
vjpilp Beginner

Joined: 01 May 2004 Posts: 8 Topics: 2
|
Posted: Fri Jun 25, 2004 12:50 pm Post subject: |
|
|
I think you should try
Code: |
UPDATE T2
SET T2.OrderType = (
SELECT COALESCE(T1.OrderType,'XXX')
FROM T1
WHERE T1.OrderNo = T2.OrderNo ) ;
|
or
Code: |
UPDATE T2
SET T2.OrderType = (
SELECT VALUE(T1.OrderType,'XXX')
FROM T1
WHERE T1.OrderNo = T2.OrderNo ) ;
|
Both VALUE and COALESCE essentially perform the same function. If the first parm is NULL then it will return the value 'XXX', Try it out and let us know if it worked. |
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
Posted: Fri Jun 25, 2004 12:54 pm Post subject: |
|
|
vjpilp,
The value or coalesce function will be of NO use in this case. Because , the order_no is found in the table 1. You will never get a null value on the sub-select
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
 |
psridhar Beginner
Joined: 16 May 2004 Posts: 68 Topics: 26
|
Posted: Fri Jun 25, 2004 1:08 pm Post subject: |
|
|
Hi Kolusu,
You are right. Those functions did not help me. Can you please throw some light on that are those functions and when they are useful? |
|
Back to top |
|
 |
vjpilp Beginner

Joined: 01 May 2004 Posts: 8 Topics: 2
|
Posted: Fri Jun 25, 2004 1:10 pm Post subject: |
|
|
Kolusu,
My understanding was that order_no will be found in table 1 but the order_name for the order_no could be NULL. I think psriddhar wants to insert 'PPP' if ordername for the corresponding order_no in T1 is NULL. rite?
So if the subselect could produce NULL... or am I wrong? |
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
Posted: Fri Jun 25, 2004 1:22 pm Post subject: |
|
|
Sridhar and vjpilp,
The inner select has the T1 table as the driver. So your join will produce the results for all the order_no of t1 , but it does not look at order_no of t2.
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
|
Back to top |
|
 |
psridhar Beginner
Joined: 16 May 2004 Posts: 68 Topics: 26
|
Posted: Fri Jun 25, 2004 1:28 pm Post subject: |
|
|
Hi vjpilp,
Table T1 will not have any row with Order_Name as spaces. The possibility is T2.Order_No itself may not be there in T1.
Hi Kolusu,
I tried the following SQL exactly. But still I am getting the same error.
UPDATE T2
SET T2.Order_Name = (SELECT T1.Order_Name
FROM T1
WHERE CHAR(T1.Order_No) = T2.Order_No) ;
Order_No in T2 is char(4) and in T1 is a small int.
Still getting same error......... |
|
Back to top |
|
 |
SureshKumar Intermediate
Joined: 23 Jan 2003 Posts: 211 Topics: 21
|
Posted: Fri Jun 25, 2004 1:35 pm Post subject: |
|
|
try this, I have not tested it
Code: |
UPDATE T2
SET ORDER_NAME = (SELECT CASE
WHEN ORDER_NAME IS NULL
THEN 'PPP'
ELSE
ORDER_NAME
END) AS WHATEVER
FROM T1
WHERE T1.ORDER_NO = T2.ORDER_NO) |
|
|
Back to top |
|
 |
vjpilp Beginner

Joined: 01 May 2004 Posts: 8 Topics: 2
|
Posted: Fri Jun 25, 2004 1:51 pm Post subject: |
|
|
I think suresh kumar's code will work. Just one point here - It is generally not advisable to apply scalar functions on the left hand side like
WHERE CHAR(col_no) = col2_no because all rows for col_no will be fetched and CHAR will be applied. So its an overhead on the database.
Also should it not be WHERE CAST(col_no as CHAR(4)) = whatever?  |
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
Posted: Fri Jun 25, 2004 2:02 pm Post subject: |
|
|
Suresh kumar,
Your query will not work because the inner select has the T1 table as the driver. So your join will produce the results for all the order_no of t1 , but it does not look at order_no of t2.
Sridhar
How does the the order_no in T1 & T2 look like when you select in spufi?
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
 |
|
|