Skip to main content

I need a help. In my application I need to get the first row out of the Select query execution through COBOL. I know the SELECT will return many rows but I only want the first row. I can not use Cursors as it can degrade the performance.

I need a help. In my application I need to get the first row out of the Select query execution through COBOL. I know the SELECT will return many rows but I only want the first row. I can not use Cursors as it can degrade the performance.

What database are you using and what SQL compiler directives?

If you are using OpenESQL then you can specify FETCH FIRST cursor name and it will return the first row that is returned by the cursor. You can only use FETCH with a cursor. If you just want to return a single row without using a cursor you would use a SELECT INTO statement. This is a singleton select. If more than one row is returned then only the first one will be returned.

From the documentation:

"If a SELECT INTO statement returns more than one row from the database, all rows except the first one will be discarded and sqlwarn4 will be set to "W". If you want to return more than the first row, you should use a cursor."


What database are you using and what SQL compiler directives?

If you are using OpenESQL then you can specify FETCH FIRST cursor name and it will return the first row that is returned by the cursor. You can only use FETCH with a cursor. If you just want to return a single row without using a cursor you would use a SELECT INTO statement. This is a singleton select. If more than one row is returned then only the first one will be returned.

From the documentation:

"If a SELECT INTO statement returns more than one row from the database, all rows except the first one will be discarded and sqlwarn4 will be set to "W". If you want to return more than the first row, you should use a cursor."

Thanks Chris. This was a great help...


What database are you using and what SQL compiler directives?

If you are using OpenESQL then you can specify FETCH FIRST cursor name and it will return the first row that is returned by the cursor. You can only use FETCH with a cursor. If you just want to return a single row without using a cursor you would use a SELECT INTO statement. This is a singleton select. If more than one row is returned then only the first one will be returned.

From the documentation:

"If a SELECT INTO statement returns more than one row from the database, all rows except the first one will be discarded and sqlwarn4 will be set to "W". If you want to return more than the first row, you should use a cursor."

I am using Oracle 18c database and COBSQL directive by the way


I am using Oracle 18c database and COBSQL directive by the way

If you are using COBSQL then the above doesn't apply because the syntax and behavior is defined by Oracle and not by Micro Focus.

For Oracle it appears that the fetch first 1 row only syntax is supported for a cursor.

Use Oracle FETCH to Limit Rows Returned by a Query (oracletutorial.com)

Or you can use the where rownum=1 in a select statement.

select * from table_name where rownum= 1