Subject Re: [IBO] Question about Select in DSQL
Author Helen Borrie
At 03:45 AM 07-11-02 -0800, you wrote:
>I'm trying to do a parameterized select of a single value with a DSQL object.
>
>If the select returns zero rows (instead of one), I get an error that says,
>"attempt to fetch past the last record in a record stream".
>
>Is this normal, or am I doing something wrong?

You can't perform a select query with a DSQL object. It is not a dataset
class. If you need to select a single row, use a tib_cursor (see below).


>Here's what I'm trying to accomplish. I want to do the select on a single
>column (the key). I want to know, first of all, if the key exists, and
>second, if it DOES exist, the value of a particular column. For example:
>
>select COL2 from mytable where COL1 = somevalue
>
>I want to know whether somevalue exists in the table, and if so, what the
>value of COL2 is for that row.
>
>Do I have to break it into two separate statements (a count to see if it
>exists, and then my above select to get the value), or is it possible with
>a single DSQL fetch? I know I can do this with a regular query. The
>statement always succeeds, even if it returns no rows, and I can just check
>the EOF property to see if it returned a row or not. But I want to use
>DSQL for the best performance.

Well, DSQL is no good for selecting anything, but this is the wrong way to
check for existence anyway. Testing for existence using counts is NOT the
thing to do in transactional databases; but logically you don't need to
here anyway.

Try this SQL in your ib_cursor:

SELECT COL2 FROM ATABLE
WHERE COL1 = :SOMEVALUE
(I won't use 'with ib_cursor do' because you can't do it in BCPPB)
var
Col2value: string;
...
if not ib_cursor.Prepared then
ib_cursor.Prepare;
ib_cursor.First;
if not ib_cursor.EOF then
Col2Value := ib_cursor.Fields[0].AsString
else
showmessage('This key value does not exist');
...

Helen