How to store the returned field value with the "Insert" command in a variable?

Asked

Viewed 181 times

0

I have a Procedure that performs the "Inserts" and "updates" in the tables. The need to create it was to try to centralize all check functions before entering or updating records. Today the need arose to return the "ID" field value of the table so that my application can locate the record and perform other procedures.

Stored Procedure:

SET TERM ^ ;

CREATE OR ALTER procedure sp_insupd (
    iaction varchar(3),
    iusuario varchar(20),
    iip varchar(15),
    imodulo varchar(30),
    ifieldsvalues varchar(2000),
    iwhere varchar(1000),
    idesclogs varchar(200))
returns (
    oid integer)
as
declare variable vdesc varchar(10000);
begin
  if (iaction = 'ins') then
  begin
    vdesc = idesclogs;

    /*** o erro está na linha abaixo ***/
    execute statement 'insert into '||:imodulo||' '||:ifieldsvalues||' returning ID into '||:oid||';';
  end else
  if (iaction = 'upd') then
  begin
    execute statement 'select '||:idesclogs||' from '||:imodulo||' where '||:iwhere into :vdesc;

    execute statement 'execute procedure SP_CREATE_AUDIT('''||:imodulo||''');';

    execute statement 'update '||:imodulo||' set '||:ifieldsvalues||' where '||:iwhere||';';
  end

  insert into LOGS(USUARIO, IP, MODULO, TIPO, DESCRICAO) values (
  :iusuario, :iip, :imodulo, (case :iaction when 'ins' then 1 when 'upd' then 2 end), :vdesc);
end^

SET TERM ; ^

The error on the mentioned line is occurring due to syntax error. A Procedure is compiled normally, that is, the error does not happen in the compilation, because the line in question is executed through the "execute statement". When there was no need to return the value of the "ID" field, the Procedure worked normally with the line this way:

...
execute statement 'insert into '||:imodulo||' '||:ifieldsvalues||';';
...

What would be the correct way for the "ID" field value to be stored in the "OID" variable"?

1 answer

0

I decided to publish the same question on Stack Overflow version in English because it is very urgent in a solution. I got an answer and I decided to post here in case I can help someone in the future.

The line mentioned in the code that was causing error has been replaced by the following...

execute statement 'insert into '||:modulo||' '||:ifieldsvalues||' returning ID' into :OID;

Link to question on Stack Overflow English version

Browser other questions tagged

You are not signed in. Login or sign up in order to post.