Subject Re: [ib-support] Executing DSQL inside a stored procedure
Author Helen Borrie
At 08:26 PM 07-09-02 +0200, you wrote:
>Hi, people.
>
>I have got a problem with DSQL. I have dynamically built an UPDATE statement,
>which is located inside a varchar variable. But I can't find the way to make
>Firebird execute. I am inside a stored procedure. Here is the code:
>
>[..]
>
>DECLARE VARIABLE SQL_UPDATE VARCHAR(255);
>DECLARE EXEC_UPDATE STATEMENT;
>
>[..]
>
>/* Now SQL_UPDATE contains something like
>
> UPDATE GROUPS SET
> GRP_GRPNAM = :P_GRPNAM,
> GRP_PARCOD = :P_PARCOD,
> GRP_PASSWD = :P_PASSWD
> WHERE GRP_GRPCOD = :P_GRPCOD;
>
>where :P_GRPNAM, :P_PARCOD, :P_PASSWD and :P_GRPCOD are the input stored
>procedure parameters. */
>
>PREPARE EXEC_UPDATE FROM :SQL_UPDATE;
>EXECUTE :EXEC_UPDATE;
>
>And here is the error:
>
>ISC ERROR CODE: 335544569
>ISC ERROR MESSAGE
>Dynamic SQL Error
>SQL error code = -104
>Token Unknown - line 16, char 9
>EXEC_UPDATE
>
>Line 16 is the second declaration at top, the on with "DECLARE EXEC_UPDATE
>STATEMENT" is.
>
>If I take that line out, then the error is
>
>ISC ERROR CODE: 335544569
>ISC ERROR MESSAGE:
>Dynamic SQL Error
>SQL error code = -104
>Token unknown - line 103, char 15
>EXEC_UPDATE
>
>Line 103 is the one with the PREPARE statement.
>
>This is the first time I try this (using DSQL inside a stored procedure),
>so I
>really don't know what to do.
>
>I hope any of you may help me. Meanwhile I'll read the language reference
>manual again and see if I forgot something. Thanks in advance anyway.
>
>P.S.: I also tried to use EXECUTE IMMEDIATE instead of PREPARE and EXECUTE,
>since the language reference says it, but I got a "TOKEN UNKNOWN 'IMMEDIATE'"
>error, which stopped me.

Some fundamentals: currently you can't construct and execute a statement
inside a stored proc, since Firebird needs to know everything except the
constants for the parameters at compile time. For later versions of
Firebird, Dmitry has been working on a new feature which introduces an
EXECUTE STATEMENT command into PSQL.

For now, you need to rethink your architecture. Basically, if users are
going to construct statements then your only option is DSQL calls from the
client.

Also, PREPARE is an API call, not an SQL function, so it's never available
in PSQL.

heLen