Subject Re: [firebird-support] basic cursor and firebird psql question
Author Helen Borrie
At 06:43 a.m. 13/10/2015, thirdshiftcoder@... [firebird-support] wrote:

>I have 2 questions from the code below.
>
>Where do you come up with RDB$FIELD_NAME?

It is a column in the system table RDB$RELATION_FIELDS. The RDB$-- tables exist in all Firebird databases. They store your metadata. Look in Appendix V of Vol. 2. They are useful for examples in a book, since you can query them yourself.

>How can you use :RNAME IN THE WHERE clause?

In PSQL, you can.


>Where is it getting its value from? It seems like it is just a variable in psql without being assigned a value.

You have misquoted the example text; but the variable RNAME is declared beneath the header. In the declaration of the cursor, it is a placeholder for the value that will be assigned to it in the FOR...SELECT loop (which you have misquoted).

>...
>
>AS
>
> DECLARE RNAME CHAR(31)
>
> DECLARE FNAME CHAR(31)
>
> DECLARE C CURSOR FOR
>
>(SELECT RDB$FIELD_NAME FROM RDB$RELATION_FIELDS
>
>WHERE RDB$RELATION_NAME = :RNAME
>
>ORDER BY...);

The above sets up the explicit cursor but it's not opened yet.

Keyword FOR (missing from your pruned example) sets up an implicit cursor that is
going to embed the explict one - the objective of the example

FOR

SELECT RDB$RELATION_NAME /* a value */
INTO :RNAME /* the previously declared variable */
DO

BEGIN

OPEN C; /* open the cursor, for which the variable RNAME now has a value
obtained from the FOR SELECT loop */

... /* Inside the block, the current RNAME variable enables the cursor to
fetch all the RDB$FIELD_NAME values for that relation into the
variable FNAME; the SUSPEND statement in there passes the
current FNAME value to a buffer, which the caller is retrieving with
a SELECT .... from <selectable SP> statement. */
CLOSE C;
...
END
That BEGIN...END block will be executed until the FOR loop runs out of "next"
values for the variable RNAME.
...
END

HTH,
Helen