下面是一个示例查询,我希望我的程序生成
select *
from Registration
where Loc_ID = 6
AND CROP_ID = 163
AND REG_NAME = 'Apiro MX';
REG_NAME
是varchar2()
我创建了一个过程,我想在其中执行一个查询,如下所示
query := 'select REG_ID from Registration where loc_id = ' ||
countryid || ' AND Crop_id = ' || cropid ||
' AND Reg_name = '|| ''' || productid || ''' || ';
我在REG_NAME
部分收到错误,其中productid
为" || productid ||"
你可以帮我解决一下这个确切的问题。
答案 0 :(得分:1)
您不需要使用动态sql:
CREATE PROCEDURE get_registration (
i_countryid IN REGISTRATION.LOC_ID%TYPE,
i_crop_id IN REGISTRATION.CROP_ID%TYPE,
i_reg_name IN REGISTRATION.REG_NAME%TYPE,
o_cursor OUT SYS_REFCURSOR
)
AS
BEGIN
OPEN o_cursor FOR
SELECT *
FROM Registration
WHERE Loc_ID = i_countryid
AND CROP_ID = i_crop_id
AND REG_NAME = i_reg_name;
END;
/
如果你确实需要动态SQL(但是,你几乎总是可以不用):
CREATE PROCEDURE get_registration (
i_countryid IN REGISTRATION.LOC_ID%TYPE,
i_crop_id IN REGISTRATION.CROP_ID%TYPE,
i_reg_name IN REGISTRATION.REG_NAME%TYPE,
o_cursor OUT SYS_REFCURSOR
)
AS
BEGIN
OPEN o_cursor
FOR 'SELECT *
FROM Registration
WHERE Loc_ID = :i
AND CROP_ID = :j
AND REG_NAME = :k'
USING i_countryid, i_crop_id, i_reg_name;
END;
/
答案 1 :(得分:-1)
在PLSQL中,你可以通过添加另一个来逃避引用,因此, 你应该在productid周围加上双引号。 尝试类似:
query := 'select REG_ID from Registration where loc_id = ' ||
countryid || ' AND Crop_id = ' || cropid ||
' AND Reg_name = '|| '''' || productid || '''' || ';
或者不要使用动态SQL并尝试创建存储过程 (查看指令的链接)。 http://plsql-tutorial.com/plsql-procedures.htm