Solved

GennericSearch_Routine.

Posted on 2011-09-11
8
216 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
 
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now