View previous topic :: View next topic |
Author |
Message |
Pavani Donepudi Beginner
Joined: 07 Apr 2004 Posts: 13 Topics: 7
|
Posted: Fri Jul 15, 2005 1:22 pm Post subject: Embedded SQL select with ORDER BY and FETCH FIRST |
|
|
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 |
|
 |
NASCAR9 Intermediate
Joined: 08 Oct 2004 Posts: 274 Topics: 52 Location: California
|
Posted: Fri Jul 15, 2005 4:49 pm Post subject: |
|
|
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 |
|
 |
schintala Beginner
Joined: 18 May 2005 Posts: 108 Topics: 31 Location: USA
|
Posted: Mon Jul 18, 2005 8:34 am Post subject: |
|
|
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 |
|
 |
nadh Intermediate
Joined: 08 Oct 2004 Posts: 192 Topics: 89
|
Posted: Mon Jul 18, 2005 9:11 am Post subject: |
|
|
Hi
I think your COL4 in orderby should be mentioned in SELECT CLAUSE.
Cheers!
nadh |
|
Back to top |
|
 |
schintala Beginner
Joined: 18 May 2005 Posts: 108 Topics: 31 Location: USA
|
Posted: Mon Jul 18, 2005 9:23 am Post subject: |
|
|
It is not mandatory to mention the column name in ORDER BY clause for select statement. |
|
Back to top |
|
 |
NASCAR9 Intermediate
Joined: 08 Oct 2004 Posts: 274 Topics: 52 Location: California
|
|
Back to top |
|
 |
suhas_pm Beginner

Joined: 12 Oct 2004 Posts: 3 Topics: 1 Location: Pune
|
Posted: Tue Jul 19, 2005 6:18 am Post subject: |
|
|
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 |
|
 |
Rama_Prayaga Beginner

Joined: 20 May 2003 Posts: 45 Topics: 15
|
Posted: Wed Jul 20, 2005 2:01 pm Post subject: |
|
|
Hi Pavani Donepudi,
I have couple of questions.
1) does your SQL sytnax start with EXEC SQL statement and end with Semicolon ( .
2) are you using DCLGEN variable are your own working storage variables.
Regards,
Rama |
|
Back to top |
|
 |
Rama_Prayaga Beginner

Joined: 20 May 2003 Posts: 45 Topics: 15
|
Posted: Wed Jul 20, 2005 2:06 pm Post subject: |
|
|
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 |
|
 |
|
|