来自iSQL的Oracle存储过程调用加上无效标识符
我使用以下代码在Firefox上使用iSQL Plus创建了一个过程。该过程编译成功来自iSQL的Oracle存储过程调用加上无效标识符,oracle,stored-procedures,plsql,isql,Oracle,Stored Procedures,Plsql,Isql,我使用以下代码在Firefox上使用iSQL Plus创建了一个过程。该过程编译成功 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 produc
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;
我尝试使用以下代码调用此过程
var rc refcursor
exec get_staff('A56',:rc)
print rc
我得到以下错误
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"
在这种情况下,不需要动态sql:
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,那么在大多数情况下,理想情况下您会希望绑定:
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需要用引号括起来:
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注入)