user980411
user980411

Reputation: 1279

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.

Upvotes: 11

Views: 113446

Answers (1)

Gaurav Soni
Gaurav Soni

Reputation: 6336

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;

Upvotes: 36

Related Questions