2011-06-07 36 views
5

我使用的是PL SQL过程中的以下代码:问题的PL SQL过程中使用“执行即时”

execute immediate 'select count(distinct item_type) into counter_variable 
from items where ' || field_name || ' is not null' 

这里,

counter_variable在过程 FIELD_NAME的声明部分声明是PL SQL过程的IN参数,传递的值将是'项目'表中的列名称

我得到的错误是'无效的SQL语句',我无法找出原因。有任何想法吗?

感谢

回答

10

into子句是PL/SQL和SQL语句无效。 试试这个:

execute immediate 'select count(distinct item_type) 
from items where ' || field_name || ' is not null' into counter_variable 
+3

不要忘记:如果field_name是由用户提供的,那么必须检查SQL注入。 – 2011-06-07 11:50:05