使用带有绑定变量的Execute Immediate语句时,表名称错误无效

时间:2011-03-10 12:39:01

标签: plsql oracle10g dynamic-sql execute-immediate

我正在尝试运行此动态SQL(使用EXECUTE IMMEDIATE

M_SQL_STATEMENT := 'SELECT MAX(:m_var1)+1 from :m_var2 RETURNING MAX(:m_var1)+1 INTO :m_var3';

EXECUTE IMMEDIATE M_SQL_STATEMENT
   USING M_COLUMN_NAME, UPPER(P_TABLE_NAME), M_COLUMN_NAME
   RETURNING INTO M_SEQ_NUMBER;

但是,在尝试运行时,我会继续遇到

ORA-00903: Invalid table

P_TABLE_NAME是一个表名,可以作为输入接受。我已确认表名和&列名有效。我无法弄清楚为什么Oracle会抛出错误。

FWIW将SQL语句改为

M_SQL_STATEMENT := 'SELECT MAX(:m_var1)+1 SEQ from :m_var2 RETURNING SEQ INTO :m_var3';

仍会导致相同的错误。

1 个答案:

答案 0 :(得分:7)

您需要将表名和列名放入动态SQL中,例如

M_SQL_STATEMENT := 'SELECT MAX(' || M_COLUMN_NAME || ')+1 from ' 
|| P_TABLE_NAME';

EXECUTE IMMEDIATE M_SQL_STATEMENT INTO M_SEQ_NUMBER;