[英]Use a variable with “LIKE %” (e.g. “variable%”) in PL/SQL?
The question is similar to using LIKE in SQL *PLUS , where a select statement contains a LIKE clause as follows: 问题类似于在SQL * PLUS中使用LIKE ,其中select语句包含LIKE子句,如下所示:
select * from sometable where somecolumn LIKE 'something%';
How could one use the same within a cursor? 如何在游标中使用相同的? I tried using the following:
我尝试使用以下内容:
cursor c is select * from sometable where somecolumn like 'something%';
same as above 与上述相同
EDIT: I need to get something as a parameter, meaning, the select statement is executed within a stored procedure. 编辑:我需要获取一些参数,这意味着,select语句在存储过程中执行。
EDIT 2: 编辑2:
create procedure proc1 (search VARCHAR) is
cursor c is select student_name from students where student_name like 'search%';
--I know using 'search%' retrieves student names containing 'the key search', but is there any other way to use such a variable. - 我知道使用'search%'检索包含'密钥搜索'的学生姓名,但是有没有其他方法可以使用这样的变量。
do something;
end;
In short, I need to select student names containing a value that is passed as a parameter; 简而言之,我需要选择包含作为参数传递的值的学生姓名; this may not be the whole name, and may suffice enough to be used within a like clause.
这可能不是全名,可能足以在类似的条款中使用。
As per my understanding to your issue, you are using variable search
within quotes. 根据我对您的问题的理解,您在引号内使用变量
search
。 Put your variable outside the quotes, eg: 把你的变量放在引号之外,例如:
create or replace procedure PROC1(search VARCHAR2)
IS
cursor test_cur(search IN VARCHAR2)
IS
SELECT student_name
FROM student
WHERE student_name LIKE search||'%'; --you're putting you variable within quotes
v_temp_var student.student_name%TYPE;
BEGIN
OPEN test_cur(search);
LOOP
FETCH test_cur INTO v_temp_var;
EXIT WHEN test_cur%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(v_temp_var);
END LOOP;
CLOSE test_cur;
END test;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.