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 > [DB2 ver 9.1 z/OS] IN predicate with multiple columns

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-27-11, 07:11
humble_man humble_man is offline
Registered User
 
Join Date: Nov 2011
Posts: 7
Question [DB2 ver 9.1 z/OS] IN predicate with multiple columns

Hi All,

I need to select some rows by using IN predicate with multiple columns.

Code:
SELECT TYP, ID FROM MYVIEW WHERE (TYP, ID) IN (VALUES ('1','1'),('2','2'),('3','3'))
I got an error:

Quote:
ILLEGAL SYMBOL ",". SOME SYMBOLS THAT MIGHT BE LEGAL ARE: MICROSECONDS MICROSECOND SECONDS SECOND MINUTES MINUTE HOURS. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.8.86
Is the syntax wrong ?

Can it be a JDBC driver problem ?

How can I rewrite it without using bunch of OR inside WHERE ?

Thank you,
Humble Man
Reply With Quote
  #2 (permalink)  
Old 11-27-11, 08:23
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
What are the data type of TYP and ID?
Reply With Quote
  #3 (permalink)  
Old 11-27-11, 08:34
humble_man humble_man is offline
Registered User
 
Join Date: Nov 2011
Posts: 7
Here is an excerpt from the DDL:

Quote:
"TYP" CHAR(5) FOR SBCS DATA WITH DEFAULT NULL,
"ID" CHAR(7) FOR SBCS DATA WITH DEFAULT NULL,

Last edited by humble_man; 11-27-11 at 08:39.
Reply With Quote
  #4 (permalink)  
Old 11-27-11, 08:57
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Multi row constructor "VALUES (...) , (...) , ..." is not supported on DB2 for z/OS.

So, please try to replace VALUES clause with "SELECT ... sysibm.sysdummy1 UNION ALL ...".
Reply With Quote
  #5 (permalink)  
Old 11-27-11, 09:46
humble_man humble_man is offline
Registered User
 
Join Date: Nov 2011
Posts: 7
Tonkuma, thank you.

Could you show how the original query (from first post) will look like ? I am a programmer, so having difficulties with syntax.

Is it OK to use sysibm.sysdummy1 table in production ?

Best regards,
Humble man.
Reply With Quote
  #6 (permalink)  
Old 11-27-11, 10:37
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Quote:
... I am a programmer, ...
SQL is a programming language.
If you are a programmer and you are requested to use SQL,
you should learn it and get (at least) average skill of it.

Quote:
... having difficulties with syntax.
It's a basic skill for a programmer to study a programming language.
If you couldn't read and understand C(or any other programming language) syntax,
you can't be a C expert.
You can't be more than mediocre programmer.

Quote:
Is it OK to use sysibm.sysdummy1 table in production ?
No problrm, I think.

Anyhow, try this...
Code:
SELECT typ, id
 FROM  myview
 WHERE (typ, id)
       IN (SELECT '1' , '1' FROM sysibm.sysdummy1 UNION ALL
           SELECT '2' , '2' FROM sysibm.sysdummy1 UNION ALL
           SELECT '3' , '3' FROM sysibm.sysdummy1
          )
;
Reply With Quote
  #7 (permalink)  
Old 11-27-11, 11:06
humble_man humble_man is offline
Registered User
 
Join Date: Nov 2011
Posts: 7
Thank you, Tonkuma

Could you tell what is advantage (if any) of UNION ALL and SYSDUMMY1 approach, compared to the following query:

Code:
SELECT TYP, ID FROM MYVIEW WHERE
(TYP='1' AND ID='1') OR
(TYP='2' AND ID='2') OR
(TYP='3' AND ID='3')
Thank you,
Humble Man
Reply With Quote
  #8 (permalink)  
Old 11-27-11, 11:56
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Access path might be different.
Using OR would be less chance to use index scan.

But, it depends on many factors,
for example, number of rows in a table, created(and exist) indexes, result of runstats, so on...

You can examine the access path by explain.

I don't konw much about DB2 z/OS,
so please read manuals or Information Center for tuning your query.
For example:
DB2 9 - Performance - Tuning your queries
DB2 9 - Performance - Boolean term predicates
DB2 9 - Performance - Summary of predicate processing

Last edited by tonkuma; 11-27-11 at 12:08. Reason: Add reference to "Summary of predicate processing".
Reply With Quote
  #9 (permalink)  
Old 11-27-11, 12:04
humble_man humble_man is offline
Registered User
 
Join Date: Nov 2011
Posts: 7
Thumbs up

Thank you very much !

Reply With Quote
  #10 (permalink)  
Old 11-28-11, 13:21
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
I would much prefer using a CTE, than hit against sysdummy1. In fact, I, typically, tell folks to stay away from sysdummy1 in anything other than an adhoc basis, as most times it is misused and costs you more.
Try something like:

Code:
WITH CTE(typ,id)
   AS ( VALUES
 ('1','1')
,('2,'2')
,('3','3'))
SELECT * FROM MYVIEW MV
WHERE mv.typ = cte.typ
   and mv.id = cte.id
Dave Nance
Reply With Quote
  #11 (permalink)  
Old 11-28-11, 15:25
humble_man humble_man is offline
Registered User
 
Join Date: Nov 2011
Posts: 7
Hi Dave,

Thank you for suggestions.

Unfortunately, it seems like (as pointed by Tonkuma), VALUES statement does not work on z/OS DB2 v.9.1.

The statement you posted gives an error:

Quote:
ILLEGAL SYMBOL "2". SOME SYMBOLS THAT MIGHT BE LEGAL ARE: MICROSECONDS MICROSECOND SECONDS SECOND MINUTES MINUTE HOURS. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.8.86
Reply With Quote
Reply

Tags
in predicate

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