DBAsupport.com Forums - Powered by vBulletin
Results 1 to 4 of 4

Thread: Passing a variable into 'like' condition in a query

  1. #1
    Join Date
    Dec 2000
    Posts
    87
    Hi all,

    Does anyone know how to pass a variable into 'like' condition in a query?

    This is what I'm trying to achieve. Say if I have a procedure:

    ****************************
    create or replace procedure test
    (searchstr varchar2)

    IS
    y varchar2(20)
    Begin
    insert into y
    select name from employee where name like '%searchstr%';
    dbms_output.put_line(y);
    End;
    ***************************

    Here searchstr is a input parameter from user. If searchstr='dav', then I want 'dav' to be plugged-in to the like condition.
    I know the above isn't right, so what's the correct syntax?
    Thanks


  2. #2
    Join Date
    Feb 2001
    Posts
    66
    try this

    select name into y from employee where name like '%' || searchstr || '%' ;

  3. #3
    Join Date
    Dec 2000
    Posts
    87
    Thanks I'll try that.

  4. #4
    Join Date
    Mar 2001
    Posts
    22

    use of variable

    Simply declare a variable to hold the input string and execute the sql.

    example:

    create table exptest as select * from all_users;
    delete exptest ;
    declare
    b varchar2(10);
    begin
    b := '%A%';
    insert into exptest select * from all_users where username like b;
    end;
    /

    hptse

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Click Here to Expand Forum to Full Width