【问题标题】:Problem using 'execute immediate' in PL SQL Procedure在 PL SQL 过程中使用“立即执行”的问题
【发布时间】:2011-06-07 19:49:58
【问题描述】:

我在 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 参数,传递的值将是“items”表中的列名

我得到的错误是“无效的 SQL 语句”,我无法找出原因。有什么想法吗?

谢谢

【问题讨论】:

    标签: sql oracle plsql


    【解决方案1】:

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

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

    【讨论】:

    • 不要忘记:如果 field_name 由用户提供,则必须检查 SQL 注入。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-09
    • 1970-01-01
    • 1970-01-01
    • 2017-02-14
    • 2023-03-31
    • 2018-02-25
    相关资源
    最近更新 更多