If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > To check for the non existence in a table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-07-09, 08:48
laknar laknar is offline
Registered User
 
Join Date: Jul 2008
Posts: 80
To check for the non existence in a table

Need to check for the non existence in a table

Code:
CREATE TABLE TABLE1(COLUMN1 VARCHAR(20),COLUMN2 VARCHAR(20),COLUMN3 VARCHAR(20))

INSERT INTO TABLE1 VALUES('A','W','SEAT');
INSERT INTO TABLE1 VALUES('B','S','SEAT1');
INSERT INTO TABLE1 VALUES('C','S','SEAT1');
INSERT INTO TABLE1 VALUES('D','S','SEAT1');
INSERT INTO TABLE1 VALUES('A','W','AFGH');

select DISTINCT CASE WHEN (select 'TRUE' from TABLE1 WHERE NOT EXISTS (select * from TABLE1 WHERE COLUMN2<>'S' OR COLUMN2<>'W'))='TRUE' THEN 'TRUE' ELSE 'FALSE' END FROM TABLE1 ;

Im trying the above query but not getting result as expected.
Reply With Quote
  #2 (permalink)  
Old 04-07-09, 09:27
umayer umayer is offline
Registered User
 
Join Date: Dec 2005
Posts: 273
WHERE COLUMN2<>'S' OR COLUMN2<>'W'

this is always true as long as column2 isn't NULL




Do you mean a query like this:

SELECT CASE WHEN CNT = 0 THEN 'TRUE' ELSE 'FALSE' END FROM
(select count(*) AS CNT from TABLE1 WHERE COLUMN2<>'S' AND COLUMN2<>'W') A

Last edited by umayer; 04-07-09 at 09:37.
Reply With Quote
  #3 (permalink)  
Old 04-07-09, 10:15
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Another variation.

Code:
------------------------------ Commands Entered ------------------------------
SELECT CASE
       WHEN NOT EXISTS
          ( select * from TABLE1
             WHERE COLUMN2 NOT IN ('S', 'W')
          ) THEN
            'TRUE'
       ELSE 'FALSE'
       END  "Answer is"
  FROM SYSIBM.SYSDUMMY1;
------------------------------------------------------------------------------

Answer is
---------
TRUE     

  1 record(s) selected.
Reply With Quote
  #4 (permalink)  
Old 04-07-09, 23:55
laknar laknar is offline
Registered User
 
Join Date: Jul 2008
Posts: 80
hi tonkuma,umayer both the queries worked perfectly.
thank you.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On