View previous topic :: View next topic |
Author |
Message |
yadav2005 Intermediate

Joined: 10 Jan 2005 Posts: 348 Topics: 144
|
Posted: Thu May 26, 2011 9:44 am Post subject: Unable to create View with FETCH FIRST N ROWS ONLY |
|
|
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 |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
Posted: Thu May 26, 2011 10:33 am Post subject: |
|
|
yadav2005,
FETCH FIRST clause is supported in z/OS DB2 V9 and higher. What version of DB2 are you running?
Kolusu |
|
Back to top |
|
 |
yadav2005 Intermediate

Joined: 10 Jan 2005 Posts: 348 Topics: 144
|
Posted: Thu May 26, 2011 10:47 am Post subject: |
|
|
Kolusu,
I am in DB2 V8 only . Is there any way to achieve the same. |
|
Back to top |
|
 |
dbzTHEdinosauer Supermod
Joined: 20 Oct 2006 Posts: 1411 Topics: 26 Location: germany
|
Posted: Fri May 27, 2011 12:53 am Post subject: |
|
|
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 |
|
 |
dbzTHEdinosauer Supermod
Joined: 20 Oct 2006 Posts: 1411 Topics: 26 Location: germany
|
Posted: Fri May 27, 2011 2:46 am Post subject: |
|
|
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 |
|
 |
GuyC Supermod
Joined: 27 May 2011 Posts: 35 Topics: 0
|
Posted: Mon May 30, 2011 1:53 am Post subject: |
|
|
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 |
|
 |
dbzTHEdinosauer Supermod
Joined: 20 Oct 2006 Posts: 1411 Topics: 26 Location: germany
|
Posted: Mon May 30, 2011 2:17 am Post subject: |
|
|
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 |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
|
Back to top |
|
 |
GuyC Supermod
Joined: 27 May 2011 Posts: 35 Topics: 0
|
Posted: Tue May 31, 2011 3:38 am Post subject: |
|
|
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 |
|
 |
GuyC Supermod
Joined: 27 May 2011 Posts: 35 Topics: 0
|
Posted: Tue May 31, 2011 3:41 am Post subject: |
|
|
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 |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
Posted: Tue May 31, 2011 10:54 am Post subject: |
|
|
GuyC,
I have granted edit authority of your posts.
Kolusu |
|
Back to top |
|
 |
|
|