我是oracle的新手,并尝试使用以下pl / sql代码创建存储过程。
代码有什么问题?当我用硬编码值'1605%'替换searchString时,执行查询但不执行变量。
create or replace PACKAGE BODY MAT_INFO AS
PROCEDURE mat_details(mat_id IN varchar, mat_cur OUT SYS_REFCURSOR) AS
searchString varchar2(700);
BEGIN
searchString := ''''||mat_id||'%''';
OPEN mat_cur FOR SELECT mi.* FROM S_JAMM_MAT_INFO mi WHERE mi.MAT_ID LIKE searchString;
END mat_details;
END MAT_INFO;
任何人都可以帮助我,我在这里做错了吗?
由于
答案 0 :(得分:2)
您使用的是不必要的引号:
SQL> declare
2 mat_id varchar2(100) := 'X';
3 searchString varchar2(100) := ''''||mat_id||'%''';
4 searchString2 varchar2(100) := ''||mat_id||'%';
5 num number;
6 begin
7 select count(1)
8 into num
9 from dual
10 where 'X' like searchString;
11 --
12 dbms_output.put_line('1. num= ' || num);
13 --
14 select count(1)
15 into num
16 from dual
17 where 'X' like searchString2;
18 --
19 dbms_output.put_line('2. num= ' || num);
20 end;
21 /
1. num= 0
2. num= 1
PL/SQL procedure successfully completed.
SQL>
如果要使用动态SQL,那么双引号可能很有用,但在静态查询中,这意味着您正在寻找包含引号作为要查找的字符串一部分的模式。
答案 1 :(得分:0)
create or replace PACKAGE BODY MAT_INFO AS
PROCEDURE mat_details(
i_mat_id IN varchar,
mat_cur OUT SYS_REFCURSOR
)
AS
BEGIN
OPEN mat_cur FOR
SELECT *
FROM S_JAMM_MAT_INFO
WHERE MAT_ID LIKE i_mat_id || '%';
END mat_details;
END MAT_INFO;
/
答案 2 :(得分:-1)
您可以尝试更改以下过程。
CREATE OR REPLACE PACKAGE BODY MAT_INFO AS
PROCEDURE MAT_DETAILS(MAT_ID IN VARCHAR, MAT_CUR OUT SYS_REFCURSOR) AS
SEARCHSTRING VARCHAR2(700);
CURSOR MAT_CUR (SEARCHSTRING VARCHAR) IS
SELECT MI.* FROM S_JAMM_MAT_INFO MI WHERE MI.MAT_ID LIKE SEARCHSTRING;
BEGIN
FOR DATA IN MAT_CUR(MAT_ID)
LOOP
--YOUR CODE
END LOOP;
END MAT_DETAILS;
END MAT_INFO;