How to reference firled names with variable in Fir

2019-01-29 02:17发布

Please give me an example how to reference field names with variable in 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

Thanks

标签: sql firebird
1条回答
forever°为你锁心
2楼-- · 2019-01-29 03:16

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^
查看更多
登录 后发表回答