在PL SQL过程中使用'execute immediate'的问题

tus*_*say 5 sql oracle plsql

我在PL SQL过程中使用以下代码:

execute immediate 'select count(distinct item_type) into counter_variable
from items where ' || field_name || ' is not null'
Run Code Online (Sandbox Code Playgroud)

这里,

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

我得到的错误是"无效的SQL语句",我无法弄清楚原因.有任何想法吗?

谢谢

Kla*_*äck 10

into子句是PL/SQL,在SQL语句中无效.试试这个:

execute immediate 'select count(distinct item_type) 
from items where ' || field_name || ' is not null' into counter_variable
Run Code Online (Sandbox Code Playgroud)

  • 不要忘记:如果用户提供了field_name,则必须检查SQL注入. (3认同)