首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >来自iSQL的Oracle存储过程调用加上无效标识符

来自iSQL的Oracle存储过程调用加上无效标识符
EN

Stack Overflow用户
提问于 2013-01-23 13:44:13
回答 1查看 511关注 0票数 0

我已经使用下面的代码在火狐上使用iSQL +创建了一个过程。该过程编译成功。

代码语言:javascript
复制
create or replace procedure get_staff  (
    product_no in varchar2,
    o_cursor out sys_refcursor)
is
begin
        open o_cursor for
        'select sr.name, sr.bonus from sales_staff sr inner join product p on p.sales_staff_id = sr.staff_id where product_no = ' || product_no ;
end;

我正在尝试使用以下代码调用此过程

代码语言:javascript
复制
var rc refcursor
exec get_staff('A56',:rc)
print rc

我得到以下错误。

代码语言:javascript
复制
ERROR at line 1: 
ORA-00904: "A56": invalid identifier 
ORA-06512: at "AA2850.GET_STAFF", line 6 
ORA-06512: at line 1 
ERROR: 
ORA-24338: statement handle not executed 
SP2-0625: Error printing variable "rc" 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-23 15:36:11

在这种情况下,不需要动态sql:

代码语言:javascript
复制
open o_cursor for
        select sr.name, sr.bonus 
          from sales_staff sr 
               inner join product p 
                       on p.sales_staff_id = sr.staff_id
         where p.product_no = product_no;

如果您使用的是动态SQL,那么在大多数情况下,理想情况下您会希望绑定:

代码语言:javascript
复制
open o_cursor for
        'select sr.name, sr.bonus 
          from sales_staff sr 
               inner join product p 
                       on p.sales_staff_id = sr.staff_id
         where p.product_no = :b1' using product_no;

如果做不到这一点(在边缘情况下,有时您希望避免不正确数据的绑定变量),varchar2s需要用引号括起来:

代码语言:javascript
复制
open o_cursor for
        'select sr.name, sr.bonus 
          from sales_staff sr 
               inner join product p 
                       on p.sales_staff_id = sr.staff_id
         where p.product_no = ''' ||product_no||'''';

但您应该转义单引号,并验证product_no没有分号等(即小心SQL注入)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14473220

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档