MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Update with Not Null
Goto page 1, 2  Next
 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
psridhar
Beginner


Joined: 16 May 2004
Posts: 68
Topics: 26

PostPosted: Fri Jun 25, 2004 12:06 pm    Post subject: Update with Not Null Reply with quote

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 ....... Crying or Very sad


Sri
Back to top
View user's profile Send private message
SureshKumar
Intermediate


Joined: 23 Jan 2003
Posts: 211
Topics: 21

PostPosted: Fri Jun 25, 2004 12:31 pm    Post subject: Reply with quote

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
View user's profile Send private message
psridhar
Beginner


Joined: 16 May 2004
Posts: 68
Topics: 26

PostPosted: Fri Jun 25, 2004 12:43 pm    Post subject: Reply with quote

I am sorry...... It is OrderName
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Fri Jun 25, 2004 12:43 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
psridhar
Beginner


Joined: 16 May 2004
Posts: 68
Topics: 26

PostPosted: Fri Jun 25, 2004 12:46 pm    Post subject: Reply with quote

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
View user's profile Send private message
vjpilp
Beginner


Joined: 01 May 2004
Posts: 8
Topics: 2

PostPosted: Fri Jun 25, 2004 12:50 pm    Post subject: Reply with quote

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
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Fri Jun 25, 2004 12:54 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
psridhar
Beginner


Joined: 16 May 2004
Posts: 68
Topics: 26

PostPosted: Fri Jun 25, 2004 1:08 pm    Post subject: Reply with quote

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
View user's profile Send private message
vjpilp
Beginner


Joined: 01 May 2004
Posts: 8
Topics: 2

PostPosted: Fri Jun 25, 2004 1:10 pm    Post subject: Reply with quote

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
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Fri Jun 25, 2004 1:22 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Fri Jun 25, 2004 1:25 pm    Post subject: Reply with quote

Sridhar,

Check this link for an explanation of NULLIF and IFNULL scalar functions

http://publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/BOOKS/DSNSQH11/3.2?DT=20010718164132

Hope this helps...

Cheers

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
psridhar
Beginner


Joined: 16 May 2004
Posts: 68
Topics: 26

PostPosted: Fri Jun 25, 2004 1:28 pm    Post subject: Reply with quote

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
View user's profile Send private message
SureshKumar
Intermediate


Joined: 23 Jan 2003
Posts: 211
Topics: 21

PostPosted: Fri Jun 25, 2004 1:35 pm    Post subject: Reply with quote

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
View user's profile Send private message
vjpilp
Beginner


Joined: 01 May 2004
Posts: 8
Topics: 2

PostPosted: Fri Jun 25, 2004 1:51 pm    Post subject: Reply with quote

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? Idea
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Fri Jun 25, 2004 2:02 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group