Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamic where clause

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
i am trying to write a program that will accept a dynamic number of user inputs for one parameter. for example
if a user inputs 'smith, jones, williams'

my program will break this string up and query the database for all of the entries relating to these 3 employees.

does anyone know how id create a cursor thatd accpet a dynamic number of inputs for a parameter like this. is there a way to do a loop and have something like

'AND emp_name = x'

keep getting added on to the end of the where clause?

while x keeps changing as i loop though and parse the string.

can anyone help a brutha out?

Thanks.

lance

 
You should construct
Code:
 AND (emp_name = x OR emp_name=y OR ...)
, and then execute dynamically the constructed string.
Instead of using the
Code:
 execute immediate
statement, you'll need a ref cursor, and the next syntax:
Code:
DECLARE
   TYPE EmpCurTyp IS REF CURSOR;  -- define weak REF CURSOR type
   emp_cv   EmpCurTyp;  -- declare cursor variable
   my_ename VARCHAR2(15);
   my_sal   NUMBER := 1000;
BEGIN
   OPEN emp_cv FOR  -- open cursor variable
      'SELECT ename, sal FROM emp WHERE sal > :s' USING my_sal;
   ...
END;
Fetching is something like that:
Code:
LOOP
   FETCH emp_cv INTO my_ename, my_sal;  -- fetch next row
   EXIT WHEN emp_cv%NOTFOUND;  -- exit loop when last row is fetched
   -- process row
END LOOP;
CLOSE emp_cv;

Hope this helps
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top