1

我正在尝试在存储过程中执行一些 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

结果是Token unknown ...... into.

谁能告诉我它有什么问题?

4

1 回答 1

1

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

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

另请参阅EXECUTE STATEMENT文档

于 2013-04-08T07:04:58.990 回答