Use a variable with “LIKE %” (e.g. “variable%”) in PL/SQL?

落花浮王杯 提交于 2020-05-12 20:32:07

问题


The question is similar to using LIKE in SQL *PLUS, where a select statement contains a LIKE clause as follows:

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.

EDIT 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.

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.


回答1:


As per my understanding to your issue, you are using variable search within quotes. Put your variable outside the quotes, e.g.:

 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;


来源:https://stackoverflow.com/questions/10150723/use-a-variable-with-like-e-g-variable-in-pl-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!