5

我试图让这个动态 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';

仍然导致相同的错误。

4

1 回答 1

8

您需要将表名和列名放入动态 SQL 中,因此类似于

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

EXECUTE IMMEDIATE M_SQL_STATEMENT INTO M_SEQ_NUMBER;
于 2011-03-10T12:52:23.080 回答