Miguel Martins
Miguel Martins

Reputation: 13

Execute string as SQL query in C

it's possible to execute a string as a SQL query in C and get the result? I'm trying to compile this code:

EXEC SQL
    EXECUTE IMMEDIATE : sql_formula INTO :sql_prec_desconto_mp;

But I've this error:

Error at line 10548, column 35 in file

D:\syncs\AIX\fuentes\funcn\niv2\src\rutin as.pc

            EXECUTE IMMEDIATE : sql_formula INTO :sql_prec_desconto_mp;

PCC-S-02201, Encountered the symbol "INTO" when expecting one of the following:

; ( [ . ++ -- ->

How can I get the result of the SQL query? If I remove the INTO clause I can compile without errors.

Thanks in advance.

Upvotes: 1

Views: 380

Answers (1)

Alex Poole
Alex Poole

Reputation: 191235

That format is described in the documentation:

To prepare and execute a DELETE, INSERT, or UPDATE statement or a PL/SQL block containing no host variables.

You aren't doing one of those DML types, and those don't recognise INTO. When you run a dynamic SQL query without an INTO the query is never actually executed (see the note in the docs).

The quickest way that immediately comes to mind to do what I think you want, is to prepare a cursor and fetch the result:

EXEC SQL PREPARE stmt FROM :sql_formula;
EXEC SQL DECLARE cur CURSOR FOR stmt;
EXEC SQL OPEN cur;
EXEC SQL FETCH cur INTO :sql_prec_desconto_mp;

Upvotes: 1

Related Questions