How to reference field names with variable in Firebird stored procedure or execution block

牧云@^-^@ 提交于 2019-12-18 09:31:05

问题


Please give me an example how to reference field names with variable in a Firebird stored procedure or execute block

Something like this pseudo SQL:

Insert into tab1 (1, f1, f2, f3)
    select 1, tab2.f+var_loop, tab2.f+var_loop, tab2.f+var_loop
    from tab2
    where .....

where "f" is the first initial of the field name and "var_loop" is a loop variable


回答1:


It's still not quite clear to me what you want to achieve, but in the PSQL there is also EXECUTE STATEMENT feature available which might suit your needs - it allows you to buid a string and then execute it as a DSQL statement... Assuming the var_loop in your example is integer your code might be something like

CREATE PROCEDURE Foo(var_loop INTEGER)
AS
DECLARE Stmnt VARCHAR(1024);
BEGIN
  Stmnt = 'Insert into tab1 (1, f1, f2, f3)'||
          'select 1, tab2.f'|| CAST(var_loop AS VARCHAR(10)) ||
          ', tab2.f'|| CAST(var_loop AS VARCHAR(10)) ||
          ', tab2.f'|| CAST(var_loop AS VARCHAR(10)) ||
          'from tab2 where(...)';
  EXECUTE STATEMENT Stmnt;
END^


来源:https://stackoverflow.com/questions/7718748/how-to-reference-field-names-with-variable-in-firebird-stored-procedure-or-execu

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!