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 

Unable to create View with FETCH FIRST N ROWS ONLY

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
yadav2005
Intermediate


Joined: 10 Jan 2005
Posts: 348
Topics: 144

PostPosted: Thu May 26, 2011 9:44 am    Post subject: Unable to create View with FETCH FIRST N ROWS ONLY Reply with quote

I have two base tables with millions of records.The view is a simple select of columns from the two different tables with a where condition.With the count of records being huge,I am trying to create a View which should select first 10 rows only but I am failing with the error -109.Is there a way to solve this problem:
Code:

CREATE VIEW V1                                                                 
(                                                                               
EMPNO,                                                                         
EMPDEPT                                                                         
)                                                                               
AS                                                                             
SELECT                                                                         
A.EMPNO,                                                                       
B.EMPDEPT                                                                       
FROM                                                                           
TEST.TAB1 A,                                                                   
TEST.TAB2 B                                                                   
WHERE A.EMPNO = B.EMPNO                                                         
FETCH FIRST 10 ROWS ONLY;                                                       
---------+---------+---------+---------+---------+---------+---------+---------+
DSNT408I SQLCODE = -109, ERROR:  FETCH FIRST CLAUSE IS NOT PERMITTED           
DSNT418I SQLSTATE   = 42601 SQLSTATE RETURN CODE                               
DSNT415I SQLERRP    = DSNHSM6 SQL PROCEDURE DETECTING ERROR                     
DSNT416I SQLERRD    = 2605 0  0  -1  1  0 SQL DIAGNOSTIC INFORMATION           
DSNT416I SQLERRD    = X'00000A2D'  X'00000000'  X'00000000'  X'FFFFFFFF'       
         X'00000001'  X'00000000' SQL DIAGNOSTIC INFORMATION                   
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: Thu May 26, 2011 10:33 am    Post subject: Reply with quote

yadav2005,

FETCH FIRST clause is supported in z/OS DB2 V9 and higher. What version of DB2 are you running?

Kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
yadav2005
Intermediate


Joined: 10 Jan 2005
Posts: 348
Topics: 144

PostPosted: Thu May 26, 2011 10:47 am    Post subject: Reply with quote

Kolusu,

I am in DB2 V8 only . Is there any way to achieve the same.
Back to top
View user's profile Send private message
dbzTHEdinosauer
Supermod


Joined: 20 Oct 2006
Posts: 1411
Topics: 26
Location: germany

PostPosted: Fri May 27, 2011 12:53 am    Post subject: Reply with quote

i may be way off base here, but:

is not a view only a description of data,
that does not exist until sql is invoked to select from the view?

I don't think any version of db2 would allow a FETCH clause in the declare/Create,
otherwise the view would not be a view, but instead a populated results table,
which a VIEW is not.

now, if the idea is to insure/restrict the 'base' view to only contain a few rows,
then olap functions / row number could be in the WHERE Clause - possibly.

don't have much experience with views (will experiment later)
but, even if a FETCH first n rows or ONLY,
which you are attempting to be a part of the VIEW definition (CREATE),
would still mean that the VIEW would be materialised (wrong terminology, probably)
with the millions of rows and then the FETCH restrictions would be applied.

and since your goal is to have the VIEW only FETCH (materialize) a few rows,
they would always be the same,
so why not restrict them in the CREATE VIEW WHERE clause with EMPNO < (or not gt) a literal value?
_________________
Dick Brenholtz
American living in Varel, Germany
Back to top
View user's profile Send private message
dbzTHEdinosauer
Supermod


Joined: 20 Oct 2006
Posts: 1411
Topics: 26
Location: germany

PostPosted: Fri May 27, 2011 2:46 am    Post subject: Reply with quote

ok, I received a -4700 error: attempt to use new function before new function mode.

so, I can only assume that my previous post is BS, and you can use the FETCH clause in a create view construct. i am at db2 9, obviously without new function mode.

so, not being able to test - disregard my previous post.

dbz
_________________
Dick Brenholtz
American living in Varel, Germany
Back to top
View user's profile Send private message
GuyC
Supermod


Joined: 27 May 2011
Posts: 35
Topics: 0

PostPosted: Mon May 30, 2011 1:53 am    Post subject: Reply with quote

No, your first post was correct:
Quote:
-20211 THE SPECIFICATION ORDER BY OR FETCH FIRST N ROWS ONLY IS INVALID

Explanation:
An ORDER BY or FETCH FIRST n ROWS ONLY clause is not allowed in:
o the outer fullselect of a view definition
o a materialized query table definition
o a subselect, that is not the outermost fullselect, that is not enclosed in parentheses
Back to top
View user's profile Send private message
dbzTHEdinosauer
Supermod


Joined: 20 Oct 2006
Posts: 1411
Topics: 26
Location: germany

PostPosted: Mon May 30, 2011 2:17 am    Post subject: Reply with quote

GuyC,
welcome to the forum and thanks for reminding us,
when all else fails, read the manual.
_________________
Dick Brenholtz
American living in Varel, Germany
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: Mon May 30, 2011 9:36 am    Post subject: Reply with quote

GuyC,

Hmm where did the error code -20211 show up?

Dbz

Here is a piece of info which talks about ORDER BY and FETCH FIRST in Subselects [DB2 9 for z/OS]

http://db2portal.blogspot.com/2007/04/order-by-and-fetch-first-in-subselects.html

If you want to test it out on UDB version here is an apar description of creating views with fetch first

https://www.ibm.com/support/docview.wss?uid=swg1IY82816


Kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
GuyC
Supermod


Joined: 27 May 2011
Posts: 35
Topics: 0

PostPosted: Tue May 31, 2011 3:38 am    Post subject: Reply with quote

it's a DB2 for z/OS v9 NFM message.
And it's not about subselects, it's on the outer select of a view that it isn't allowed.

Although following selects return the same result, the first is allowed, the second isn't :
Code:
create view V1 as select col1,col2 from (select * from test1 fetch first 10 rows only) A
is allowed
Code:
Create view V2 as
select col1,col2 from  test1 fetch first 10 rows only
is not allowed

The reason is "where-clause redistribution"
Code:
Select * from V1 where col1 < 10
will be rewritten as :
Code:
Select * from (select * from test1 fetch first 10 rows only)  where col1 < 10

which is clearly different from :
Code:
Select * from V2 where col1 < 10
which is equivalent to :
Code:
Select * from test1 where col1 < 10 fetch first 10 rows only


Last edited by GuyC on Wed Jun 01, 2011 2:21 am; edited 1 time in total
Back to top
View user's profile Send private message
GuyC
Supermod


Joined: 27 May 2011
Posts: 35
Topics: 0

PostPosted: Tue May 31, 2011 3:41 am    Post subject: Reply with quote

some errors in previous post where I forgot to change the column names and tables, but I guess editing ones own post isn't allowed on this forum.
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: Tue May 31, 2011 10:54 am    Post subject: Reply with quote

GuyC,

I have granted edit authority of your posts.


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
Page 1 of 1

 
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