?
Solved

GennericSearch_Routine.

Posted on 2011-09-11
8
Medium Priority
?
258 Views
Last Modified: 2012-05-12
I have a procedure that handles generic searches submitted by user.
User can enter name, hiredate or salary or stock number.


All the data can be fetched from EMP table except when user enters a "Stock Number". If he does, then query needs to lookup in EMP_ORDERS_DETAILS to search for records that match the stock number. For example, if user entered "DVD100", I would need to find all customers who placed orders with that stock number "DVD100".

Would it best to create another query string for this condition (when user enters this field) that JOINS both table EMP and EMP_ORDER_DETAILS like this

--Add this to declaration
l_query_detail varchar2(512) default 'select * from emp a, emp_order_details b where ...'

--Addd this after begin
if (p_stock_no is NOT NULL) then
l_query := l_query_detail;
end if;

if ( p_stock_no is NOT NULL )
then
  l_query := l_query ||‘ and stock_no =:p_stock_no'‘;
else
  l_query := l_query || ‘ and (1=1 or :p_stock_no is null) ‘;
end if;

....continue code below as normal



create or replace procedure my_new_procedure
( p_ename in varchar2 default NULL,
p_hiredate in date default NULL,
p_sal in number default NULL)
as
l_cursor sys_refcursor;
l_query varchar2(512) default ‘select * from emp’;

cursor l_template is select * from emp;
l_rec l_template%rowtype;
begin
if ( p_ename is NOT NULL )
then
  l_query := l_query ||‘ where ename like ‘’%’’||:p_ename||’’%’’ ‘;
  else
l_query := l_query ||‘ where (1=1 or :p_ename is null) ‘;
end if;

if ( p_hiredate is NOT NULL )
then
  l_query := l_query ||‘ and hiredate > :p_hiredate ‘;
else
  l_query := l_query || ‘ and (1=1 or :p_hiredate is null) ‘;
end if;

if ( p_sal is NOT NULL )
then
  l_query := l_query || ‘ and sal > :p_sal ‘;
else
  l_query := l_query || ‘ and (1=1 or :p_sal is null) ‘;
end if;

dbms_output.put_line ( l_query );

open l_cursor for l_query using p_ename, p_hiredate, p_sal;
loop
fetch l_cursor into l_rec;
exit when l_cursor%notfound;
dbms_output.put_line ( l_rec.ename || ‘,’ || l_rec.hiredate || ‘,’ || l_rec.sal );
end loop;
close l_cursor;

end;
/

Open in new window

0
Comment
Question by:sam15
  • 5
  • 3
8 Comments
 
LVL 15

Expert Comment

by:x77
ID: 36520037
You work with strings, then oracle always need parse your sentence.
Then two solutions are identicals on performance.

Note that you can declare cursors and execute it with "Using" Clause for parameters.

Then cursors are precompiled and Oracle do´nt need parse.
0
 

Author Comment

by:sam15
ID: 36520533
since the query is not known until compile time, you have to start using a string and then add to it based on user inputs.
But since cursor is using bind variables it should parse only once and run thousands of times later.
0
 
LVL 15

Expert Comment

by:x77
ID: 36520811
You says have 3 cases, then you can declare 3 cursors and select the apropiate cursor depending on selected parameter.

Also, when your query is not deterministic using parameters do the possibility for Oracle Cache Find Sql Sentences more probable.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 15

Expert Comment

by:x77
ID: 36521023
You can also define a more complex Sql sentence that allow omited parameter:

 
SQL> DEFINE UBTEC=NA
SQL> DEFINE SUP=JLL
SQL> DEFINE TORD=WS
SQL> Select codtra,destra,sup,tord,ubtec from r18td 
  2  where   ('&Sup'='NA' or sup='&Sup') and
  3          ('&Tord'='NA' or Tord ='&Tord') and
  4          ('&UbTec'='NA' or UbTec='&UbTec');
old   2: where   ('&Sup'='NA' or sup='&Sup') and
new   2: where   ('JLL'='NA' or sup='JLL') and
old   3:         ('&Tord'='NA' or Tord ='&Tord') and
new   3:         ('WS'='NA' or Tord ='WS') and
old   4:         ('&UbTec'='NA' or UbTec='&UbTec')
new   4:         ('NA'='NA' or UbTec='NA')

Open in new window


Note that I use SqlPlus to build Sample and it support replacement, not parameters.
I use 'NA' for omited parameters.
When you build a query with parameters, you can code like:

   (pSup is null or pSup=Sup)                       -- Where pSup is a Plsql Var or procedure parameter

or

   (:pSup is null or :pSup=Sup)                     -- Where I use Oracle.DataAccess Command with BindByName = True
0
 
LVL 15

Expert Comment

by:x77
ID: 36521110
I Built a complex QueryCtl on Vb.Net that allows build Dynamic Query for Oracle and also for memory DataTables.
When I send a query to Oracle, always use Parameters.
It is very important for DataType compatibility and also for special character support ( embebed Quote by sample ).

 QueryCtl
For this sample, the DataTable Filter or displayed query is:

       "(T.Sup = 'JLL' And (T.Est = '8' Or T.Anulado is not null))"

But Query to Oracle with Parameters is:

       "(T.Sup = :1 And (T.Est = :2 Or T.Anulado is not null))"
0
 

Author Comment

by:sam15
ID: 36523245
if I have 3 parameters. I would need 6 CURSORS (not 3).

Think of all the combinations you can have (1 only, 2 only, 3 only, 1&2, 2&3, 1&3, 1&2&#).
If you have 4 parameters then you would need 24 cursors if you want to do that way.

0
 
LVL 15

Accepted Solution

by:
x77 earned 1500 total points
ID: 36525055
Yes, you are right.
At first time I think on alternate parameter, not combinations.

Next I propose a Complex Query that can combine any number of parameters.
0
 

Author Closing Comment

by:sam15
ID: 36571280
not fully answer question
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
Suggested Courses

588 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question