【问题标题】:Firebird Stored Procedure defining tables at runtime and returning valuesFirebird 存储过程在运行时定义表并返回值
【发布时间】:2013-04-07 23:29:49
【问题描述】:

我正在尝试在存储过程中执行一些 SQL,在运行时设置表,并返回选定的值:

create or alter procedure TEST3
returns (
    THE_VAR varchar(128))
as
declare variable TABLE_NAME varchar(64);
declare variable STMT varchar(128);
begin
  /* Procedure Text */
  Table_Name = 'users';

  stmt = 'select firstname from ' || :table_name || ' where userid = 2 into :the_var';

  execute statement stmt;

  suspend;
end

结果为@​​987654322@。

谁能告诉我这是怎么回事?

【问题讨论】:

    标签: select stored-procedures firebird


    【解决方案1】:

    您不能以这种方式使用动态 SQL 为存储过程上下文中的变量赋值。这需要从EXECUTE STATEMENT 处理。您需要将代码更改为:

    stmt = 'select firstname from ' || :table_name || ' where userid = 2';
    execute statement stmt into :the_var;
    

    另请参阅EXECUTE STATEMENT documentation

    【讨论】:

      猜你喜欢
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-15
      • 1970-01-01
      • 2014-10-15
      相关资源
      最近更新 更多