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 

Embedded SQL select with ORDER BY and FETCH FIRST

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


Joined: 07 Apr 2004
Posts: 13
Topics: 7

PostPosted: Fri Jul 15, 2005 1:22 pm    Post subject: Embedded SQL select with ORDER BY and FETCH FIRST Reply with quote

Hi,

I am trying to compile a COBOL program with the following query

SELECT COL2,
COL3
INTO :WS-COL2
,:WS-COL3
FROM TABLE1
WHERE COL1 = 12345
ORDER BY
COL4 DESC
FETCH FIRST 1 ROW ONLY

Following is the error that I got.

DSNH109I E DSNHAPLY LINE 1838 COL 14 "ORDER BY" CLAUSE NOT PERMITTED

Is it that we cannot have ORDER BY and FETCH FIRST in embedded SQLs
Back to top
View user's profile Send private message
NASCAR9
Intermediate


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Fri Jul 15, 2005 4:49 pm    Post subject: Reply with quote

Pavani Donepudi, I believe you need to create a cursor with 'FETCH FIRST XX ROWS ONLY' This is from IBM man.
EXEC SQL DECLARE C1 CURSOR FOR
SELECT * FROM X.RMT_TAB
FETCH FIRST 50 ROWS Only;

Was this a new feature for V7.x? The way I read the manual it appears that way.
NASCAR9
Back to top
View user's profile Send private message
schintala
Beginner


Joined: 18 May 2005
Posts: 108
Topics: 31
Location: USA

PostPosted: Mon Jul 18, 2005 8:34 am    Post subject: Reply with quote

I don't think you need to declare any cursor for the SQL. This SQL works well with ORDER BY CLAUSE. It must be some thing else in the program during compliation. Check the COBOL margins in the source code.
Back to top
View user's profile Send private message
nadh
Intermediate


Joined: 08 Oct 2004
Posts: 192
Topics: 89

PostPosted: Mon Jul 18, 2005 9:11 am    Post subject: Reply with quote

Hi

I think your COL4 in orderby should be mentioned in SELECT CLAUSE.

Cheers!
nadh
Back to top
View user's profile Send private message Send e-mail
schintala
Beginner


Joined: 18 May 2005
Posts: 108
Topics: 31
Location: USA

PostPosted: Mon Jul 18, 2005 9:23 am    Post subject: Reply with quote

It is not mandatory to mention the column name in ORDER BY clause for select statement.
Back to top
View user's profile Send private message
NASCAR9
Intermediate


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Mon Jul 18, 2005 10:30 am    Post subject: Reply with quote

Pavani Donepudi, I did a search(This Site) on 'fetch' and this one of the links I found:

http://www.mvsforums.com/helpboards/viewtopic.php?t=4202&highlight=fetch
NASCAR9
Back to top
View user's profile Send private message
suhas_pm
Beginner


Joined: 12 Oct 2004
Posts: 3
Topics: 1
Location: Pune

PostPosted: Tue Jul 19, 2005 6:18 am    Post subject: Reply with quote

following are the probable reasons for DSNH109I:
1. ORDER BY not allowed since result of SELECT is single row (at max)
2. SELECT used in cursor declaration can not have an INTO clause

Looks like mentioned query, if specified without 'FETCH FIRST 1 ROW ONLY' is likely to return more than one row, so most suitable solution to what is being tried to achieve may be the following: declare a cursor for this (and don't forget to remove the 'INTO' clause and also the FETCH FIRST... clause) and fetch the cursor once.

Else, write this way:
SELECT COL1,COL2
INTO <BLA>
FROM TABLE1
WHERE COL1 = 12345
AND COL4 = (SELECT MAX(COL4) FROM TABLE1 B WHERE B.COL1 = 12345)

Although this query will bomb if more than 1 rows have same max COL4 value, but feels like a better heck.

Hope this is at least somewhat useful.
-suhas
Back to top
View user's profile Send private message Yahoo Messenger
Rama_Prayaga
Beginner


Joined: 20 May 2003
Posts: 45
Topics: 15

PostPosted: Wed Jul 20, 2005 2:01 pm    Post subject: Reply with quote

Hi Pavani Donepudi,

I have couple of questions.

1) does your SQL sytnax start with EXEC SQL statement and end with Semicolon (Wink.

2) are you using DCLGEN variable are your own working storage variables.
Regards,
Rama
Back to top
View user's profile Send private message Yahoo Messenger
Rama_Prayaga
Beginner


Joined: 20 May 2003
Posts: 45
Topics: 15

PostPosted: Wed Jul 20, 2005 2:06 pm    Post subject: Reply with quote

Pavani Donepudi,

One of the reason of for DSNH109I is

Quote:
Embedded SELECT statements cannot include ORDER BY, because the result cannot be more than a single row.

Is the same thing happening in your case? try checking out this in your spufi.
Back to top
View user's profile Send private message Yahoo Messenger
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