如何从动态生成的"insert"命令中返回值?



我有一个在表中执行插入和更新的存储过程。创建它的需要是在插入或更新记录之前尝试集中所有扫描功能。今天,需要返回表的字段ID的值,以便我的应用程序可以定位注册表并执行其他存储过程。

存储过程

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;
/*** the error is on the line below ***/
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 ; ^

上一行中的错误是由于语法错误引起的。过程是正常编译的,也就是说,编译中不会发生错误,因为有问题的行是通过"execute语句"执行的。当不需要返回ID字段的值时,程序正常工作,行如下:

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

ID字段的值存储在OID变量中的正确方式是什么?

ifieldsvalues中的REAL VALUE是什么?

你不能同时拥有

  • 'insert into '||:imodulo||' '||:ifieldsvalues
  • 'update '||:imodulo||' set '||:ifieldsvalues

因为在INSERTUPDATE语句中指定列名和列值的方法根本不同!!!你要么破坏了更新stmt,要么破坏了插入stmt!

上一行中的错误是由于语法错误引起的

这还不够。显示真实的错误文本,全部。它包括您生成的实际命令,而且您似乎以错误的方式生成了它。


插入或更新记录之前的所有扫描功能

将这些函数从SQL服务器移到应用程序服务器中。然后,您就不必以"字符串拼接"的方式进行插入/更新,这是非常脆弱和"SQL注入"友好的。你走上了通往地狱的道路。

编译中不会发生错误

没错。这只是一个开始。您正在删除所有本应在应用程序开发中帮助您的安全检查。

  • http://searchsoftwarequality.techtarget.com/definition/3-tier-application
  • https://en.wikipedia.org/wiki/Multitier_architecture#Three-分层体系结构
  • http://bobby-tables.com

在现代Firebird版本上,EXECUTE STATEMENT命令可以具有与PSQLSELECT命令相同的INTO子句。

https://www.firebirdsql.org/file/documentation/reference_manuals/fblangref25-en/html/fblangref25-psql-coding.html#fblangref25-psql execstmt

使用http://translate.ru阅读http://www.firebirdsql.su/doku.php?id=execute_statement

或者只看SQL示例。但是,请注意,这些示例都使用SELECT动态命令,而不是INSERT。所以我不确定它会以这种方式工作。

这适用于Firebird 2.5(但不适用于Firebird 2.1)PSQL块。

execute statement 'insert into Z(payload) values(2) returning id' into :i;

要从IBExpert/FrameRobin/iSQL交互式shell运行它,请添加明显的样板:

execute block returns (i integer) as
begin
execute statement 'insert into Z(payload) values(2) returning id' into :i;
suspend;
end

最新更新