简体   繁体   中英

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:

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.

As per my understanding to your issue, you are using variable search within quotes. 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM