Performance tuning in SQL query using join of views

Hi,
Am trying to tune a query of the format
select ........ from view1,view2
where view1.keyfield = view2.keyfield
The base tables of the views view1 and view2 have indexes specified on 'keyfield'.
However, when I do an explain plan of the query, Full table scan of those base tables are performed.
Even tried using Hints in this query to force Oracle to use those indexes, but still not successful.
Any pointers on how to tune this kind of query would be highly welcome.
Regards,
Baish

If your query is really of the same form as you posted, then full table scans then a hash or merge join may be the most efficient plan.
You are asking for all of the rows from view1 and the matching rows from view2, because of this, Oracle dedcided that the cost of full table scans using multi-block reads is cheaper than using single block reads to get the rowids from the index, then single block reads to get the rows from the table.
FULL SCAN <> BAD
John

Similar Messages

  • About SQL Query using joins

    Hi,
    I want to write a SQL statement using Joins. The base table "A" has about 100 fields and the join table "B" a few. I would like to fetch all fields from table "A" and a few from table "B".
    Is there a way to write a query wherein I can use "Select * " for table "A" rather than writing all 100 field name in the query? Please keep in mind that I want to use a Join.
    Thanks.

    Hi Thomas,
    but if there are some fields which are in both tables, SELECT * will be not sufficient. E.g. fields ERNAM, ERDAT, AENAM, AEDAT are such fields which can occur in more tables. In case that a target area will contain these fields, by * with addition INTO CORRESPONDING FIELDS OF, you are not able to specify from which table these values should be taken. In such case, it can be solved by specifying field list dynamically. If it is not that case SELECT * is sufficient. You should check intersection of common fields for tables you are joining. Thereafter you should decide if you can use SELECT *.
    Maybe for better understanding, what exactly I am talking about, look at this example:
    TYPES: BEGIN OF ts_data,
            ernam TYPE mara-ernam,
           END OF ts_data.
    DATA: ls_data TYPE ts_data,
          ls_mara TYPE mara,
          ls_mchb TYPE mchb.
    START-OF-SELECTION.
      ls_mara-matnr = '1'.
      ls_mchb-matnr = ls_mara-matnr.
      ls_mara-ernam = 'A'.
      ls_mchb-ernam = 'B'.
      INSERT mara FROM ls_mara.
      INSERT mchb FROM ls_mchb.
      SELECT SINGLE * FROM mara INNER JOIN mchb ON mara~matnr = mchb~matnr INTO CORRESPONDING FIELDS OF ls_data.
      ROLLBACK WORK.
    You as a developer are not able to tell from which table common fields should be selected. But maybe this is really too specific case...
    Regards,
    Adrian

  • Opitmizing sql query using join

    Hello all,
    I have the following query that needs to be written using Joins. I am not sure if this is the correct place to post this question
    Tables used:
    1. activities: (a_id, n_id, market, amount, act_type, act_number, act_seq) --the money paid by the registrant for the market.
    2. market_reg (a_id, n_id, market, reg_stage) -- for each market there are registrants
    3. market (market) -- stores market details
    I need to get sum of total_payment_amount using the condition. I know the query is not opitmized and its not the correct way to get info. I was
    wondering if you guys can suggest me a query that will pull the information by using joins
    SELECT a.a_id, a.n_id, SUM (a.total_payment_amount)
    FROM activities a
    WHERE a.market = 'marketname'
    AND a.a_id||a.n_id IN
    (SELECT mr.a_id||mr.n_id
    FROM market_reg mr
    WHERE mr.market = 'marketname'
    AND mr.reg_stage = 'P'
    AND mr.n_id <> 0)
    AND (a.act_type = 'A'
    OR (a.act_type IS NULL
    AND a.act_number||act_seq IN
    ( SELECT a1.act_number||a1.act_seq
    FROM activities a1
    WHERE a1.market = 'marketname'
    GROUP BY a1.act_number||a1.act_seq
    HAVING COUNT (a1.act_number||a1.act_seq) = 1)))
    GROUP BY a.a_id, a.n_id;
    Thanks

    Hi,
    I see you have already re-posted this on a more appropriate forum, the SQL and PL/SQL forum.
    Thanks,
    Gary

  • SQL Query Using Join

    Hi,
    I am trying to join two tables using joining.
    SQL> select * from test10;
    ID
    1
    2
    SQL> select * from test11;
    ID
    1
    3
    4
    My expected output is
    ID
    1
    2
    3
    4
    I am able to do this using "Union"
    SQL> select * from test10
    2 union
    3 select * from test11;
    ID
    1
    2
    3
    4
    But i want to achieve this using join. Please help..
    Thanks & Regards,
    Arijit

    Hi, Arijit,
    The way to do that with a join is FULL OUTER JOIN, like this:
    SELECT       NVL (t10.id, t11.id)     AS id
    FROM               test10  t10
    FULL OUTER JOIN  test11  t11  ON   t10.id  = t11.id
    ORDER BY  1
    ;If that's exactly what you're trying to do, you should stick with the UNION.
    FULL OUTER JOIN is good when there are other columns involved.

  • Performance tuning of sql query with multiple joins

    My query takes at least half an hour to execute and the number of records returned are approx 1 lakh records.
    Structure of tables are:
    tblSession : ID,option1,option2,option3,option4,option5,option6,option7,option8,option9.
    tblOption : ID, labelID
    tblLabelDetail  : ID, LABELID, text
    optionID 1 to optionID9 are Foreign keys to table tblOption.ID
    My query is as below : 
    select 
    session.ID 
    ,session.tstamp
    ,session.score
    ,session.hid1
    ,session.hID2
    ,session.hID3
    ,session.collectionID
    ,session.consumerID
    ,session.langID
    ,cons_cust.text1 as    customCons_text1, 
    cons_cust.text2 as customCons_text2, 
    cons_cust.text3 as customCons_text3,
    cons_cust.text4 as customCons_text4,
    cons_cust.text5 as customCons_text5,
    cons_cust.text6 as customCons_text6,
    cons_cust.text7 as customCons_text7,
    cons_cust.text8 as customCons_text8,
    cons_cust.text9 as customCons_text9,
    ld_cons1.text as customCons_option1GUID, 
    ld_cons2.text as customCons_option2GUID, 
    ld_cons3.text as customCons_option3GUID, 
    ld_cons4.text as customCons_option4GUID ,
    ld_cons5.text as customCons_option5GUID, 
    ld_cons6.text as customCons_option6GUID, 
    ld_cons7.text as customCons_option7GUID, 
    ld_cons8.text as customCons_option8GUID, 
    ld_cons9.text as customCons_option9GUID,
    --session
    session_cust.text1 as  session_cust_text1, 
    session_cust.text2 as session_cust_text2, 
    session_cust.text3 as session_cust_text3,
    session_cust.text4 as session_cust_text4,
    session_cust.text5 as session_cust_text5,
    session_cust.text6 as session_cust_text6,
    session_cust.text7 as session_cust_text7,
    session_cust.text8 as session_cust_text8,
    session_cust.text9 as session_cust_text9,
    ld_sess1.text as session_cust_option1GUID, 
    ld_sess2.text as session_cust_option2GUID, 
    ld_sess3.text as session_cust_option3GUID, 
    ld_sess4.text as session_cust_option4GUID, 
    ld_sess5.text as session_cust_option5GUID, 
    ld_sess6.text as session_cust_option6GUID, 
    ld_sess7.text as session_cust_option7GUID, 
    ld_sess8.text as session_cust_option8GUID, 
    ld_sess9.text as session_cust_option9GUID, 
    session_cust.tStamp1,
    session_cust.tStamp2
    from mvSession session with (noexpand)
    inner join tblCollection c on c.ID=session.collectionID AND c.templateID = 405
    left join tblConsumer cons on cons.ID=session.consumerID and cons.sessionYM between 601 and 1412 and cons.sID=105
    left join vCustomConsumer cons_cust on cons_cust.sessionYM between 601 and 1412 and cons_cust.sID=105 and cons_cust.ID=cons.ID
    left join tbloption o_cons1 on o_cons1.id = cons_cust.option1 and  o_cons1.sid = 105
    left join tblLabelDetail ld_cons1 on ld_cons1.labelID = o_cons1.labelID and ld_cons1.langId = 1 and ld_cons1.eid = 107 
    left join tbloption o_cons2 on o_cons2.id = cons_cust.option2 and  o_cons2.sid = 105
    left join tblLabelDetail ld_cons2 on ld_cons2.labelID = o_cons2.labelID and ld_cons2.langId = 1 and ld_cons2.eid = 107 
    left join tbloption o_cons3 on o_cons3.id = cons_cust.option3 and  o_cons3.sid = 105
    left join tblLabelDetail ld_cons3 on ld_cons3.labelID = o_cons1.labelID and ld_cons3.langId = 1 and ld_cons3.eid = 107 
    left join tbloption o_cons4 on o_cons4.id = cons_cust.option4 and  o_cons4.sid = 105
    left join tblLabelDetail ld_cons4 on ld_cons4.labelID = o_cons4.labelID and ld_cons4.langId = 1 and ld_cons4.eid = 107 
    left join tbloption o_cons5 on o_cons5.id = cons_cust.option5 and  o_cons5.sid = 105
    left join tblLabelDetail ld_cons5 on ld_cons5.labelID = o_cons5.labelID and ld_cons5.langId = 1 and ld_cons5.eid = 107 
    left join tbloption o_cons6 on o_cons6.id = cons_cust.option6 and  o_cons6.sid = 105
    left join tblLabelDetail ld_cons6 on ld_cons6.labelID = o_cons6.labelID and ld_cons6.langId = 1 and ld_cons6.eid = 107 
    left join tbloption o_cons7 on o_cons7.id = cons_cust.option7 and  o_cons7.sid = 105
    left join tblLabelDetail ld_cons7 on ld_cons7.labelID = o_cons7.labelID and ld_cons7.langId = 1 and ld_cons7.eid = 107 
    left join tbloption o_cons8 on o_cons8.id = cons_cust.option8 and  o_cons8.sid = 105
    left join tblLabelDetail ld_cons8 on ld_cons8.labelID = o_cons8.labelID and ld_cons8.langId = 1 and ld_cons8.eid = 107 
    left join tbloption o_cons9 on o_cons9.id = cons_cust.option9 and  o_cons9.sid = 105
    left join tblLabelDetail ld_cons9 on ld_cons9.labelID = o_cons9.labelID and ld_cons9.langId = 1 and ld_cons9.eid = 107 
    left join vCustomSession session_cust on session_cust.sessionYM between 601 and 1412 and session_cust.sID=105 and session_cust.ID=session.ID
    left join tbloption o_sess1 on o_sess1.id = session_cust.option1 and  o_sess1.sid = 105
    left join tblLabelDetail ld_sess1 on ld_sess1.labelID = o_sess1.labelID and ld_sess1.langId = 1 and ld_sess1.eid = 107 
    left join tbloption o_sess2 on o_sess2.id = session_cust.option2 and  o_sess2.sid = 105
    left join tblLabelDetail ld_sess2 on ld_sess2.labelID = o_sess2.labelID and ld_sess2.langId = 1 and ld_sess2.eid = 107 
    left join tbloption o_sess3 on o_sess2.id = session_cust.option3 and  o_sess3.sid = 105
    left join tblLabelDetail ld_sess3 on ld_sess3.labelID = o_sess2.labelID and ld_sess3.langId = 1 and ld_sess3.eid = 107 
    left join tbloption o_sess4 on o_sess4.id = session_cust.option4 and  o_sess4.sid = 105
    left join tblLabelDetail ld_sess4 on ld_sess4.labelID = o_sess4.labelID and ld_sess4.langId = 1 and ld_sess4.eid = 107 
    left join tbloption o_sess5 on o_sess5.id = session_cust.option5 and  o_sess5.sid = 105
    left join tblLabelDetail ld_sess5 on ld_sess5.labelID = o_sess5.labelID and ld_sess5.langId = 1 and ld_sess5.eid = 107 
    left join tbloption o_sess6 on o_sess6.id = session_cust.option6 and  o_sess6.sid = 105
    left join tblLabelDetail ld_sess6 on ld_sess6.labelID = o_sess6.labelID and ld_sess6.langId = 1 and ld_sess6.eid = 107 
    left join tbloption o_sess7 on o_sess7.id = session_cust.option7 and  o_sess7.sid = 105
    left join tblLabelDetail ld_sess7 on ld_sess7.labelID = o_sess7.labelID and ld_sess7.langId = 1 and ld_sess7.eid = 107 
    left join tbloption o_sess8 on o_sess8.id = session_cust.option8 and  o_sess8.sid = 105
    left join tblLabelDetail ld_sess8 on ld_sess8.labelID = o_sess8.labelID and ld_sess8.langId = 1 and ld_sess8.eid = 107 
    left join tbloption o_sess9 on o_sess9.id = session_cust.option9 and  o_sess9.sid = 105
    left join tblLabelDetail ld_sess9 on ld_sess9.labelID = o_sess9.labelID and ld_sess9.langId = 1 and ld_sess9.eid = 107 
    where session.sID=105  and session.tStamp >= 'Sep  1 2014 12:00AM' and session.tStamp < 'Dec 12 2014 12:00AM'   
    order by session.tStamp, session.ID
    Is there a way , where i can simplify the joins with tbloption and tblLabelDetail and get my o/p in optimal time.
    Regards 

    I have headed towards another approach ie. using unpivot and then pivot.
    First i am converting option1-option9 into column , then doing  PIVOT to get back the same record . But issue is that when i am doing pivoting i am getting NULL values.
    My query is :
    select * into #t1  from
    select ID
    , option1
    , option2
    , option3
    , option4
    , option5
    , option6
    , option7
    , option8
    , option9
    from vCustomConsumer
    where sid=105
    and sessionYM = 1412 
    ) SourceTable
    UNPIVOT
       optionID FOR Col IN
        (option1 
    ,option2
    ,option3 
    ,option4 
    ,option5 
    ,option6 
    ,option7 
    ,option8 
    ,option9 )
    ) AS unpvt
    select t.ID,t.optionID,t.col,cast(ld.text as varchar(max)) as text into #t2
    from #t1 t
    left outer join tbloption o on o.ID =  t.optionID
    left outer join tblLabelDetail ld on ld.labelID = o.labelID and ld.langID=1 
    order by ID,col
    select ID,option1 
    ,option2
    ,[option3]
    ,option4 
    ,option5 
    ,option6 
    ,option7 
    ,option8 
    ,option9
    from
    select ID,optionID,col,text
    from #t2
    )up
    pivot 
    min(text)for col in 
    (option1 
    ,option2
    ,[option3]
    ,option4 
    ,option5 
    ,option6 
    ,option7 
    ,option8 
    ,option9
    )as pvt
    In my last query where i am using pivot, i am getting NULL values. When i check the data in temp table #t2 , it exists perfectly . But when pivoting i dont understand why it is returning most of the NULL values. I am getting data for only one column in single
    row.
    Below are some rows from result set finally obtained after pivoting :
    ID
    option1
    option2
    option3
    option4
    option5
    option6
    option7
    option8
    option9
    62949026
    NULL
    0 to 200 seconds
    NULL
    NULL
    NULL
    NULL
    NULL
    NULL
    NULL
    62966000
    NULL
    NULL
    4
    NULL
    NULL
    NULL
    NULL
    NULL
    NULL
    62966032
    NULL
    NULL
    4
    NULL
    NULL
    NULL
    NULL
    NULL
    NULL
    63090372
    NULL
    NULL
    NULL
    NULL
    EN
    NULL
    NULL
    NULL
    NULL
    63090375
    NULL
    NULL
    NULL
    NULL
    EN
    NULL
    NULL
    NULL
    NULL
    Thanks,

  • Need clear steps for doing performance tuning on SQL Server 2008 R2 (DB Engine, Reporting Services and Integration Services)

    We have to inverstigate about a reporting solution where things are getting slow (may be material, database design, network matters).
    I have red a lot in MSDN and some books about performance tuning on SQL Server 2008 R2 (or other) but frankly, I feel a little lost in all that stuff
    I'am looking for practical steps in order to do the tuning. Someone had like a recipe for that : a success story...
    My (brain storm) Methodology should follow these steps:
     Resource bottlenecks: CPU, memory, and I/O bottlenecks
     tempdb bottlenecks
     A slow-running user query : Missing indexes, statistics,...
     Use performance counters : there are many, can one give us the list of the most important
    how to do fine tuning about SQL Server configuration
    SSRS, SSIS configuration ? 
    And do the recommandations.
    Thanks
    "there is no Royal Road to Mathematics, in other words, that I have only a very small head and must live with it..."
    Edsger W. Dijkstra

    Hello,
    There is no clear defined step which can be categorized as step by step to performance tuning.Your first goal is to find out cause or drill down to factor causing slowness of SQL server it can be poorly written query ,missing indexes,outdated stats.RAM crunch
    CPU crunch so on and so forth.
    I generally refer to below doc for SQL server tuning
    http://technet.microsoft.com/en-us/library/dd672789(v=sql.100).aspx
    For SSIS tuning i refer below doc.
    http://technet.microsoft.com/library/Cc966529#ECAA
    http://msdn.microsoft.com/en-us/library/ms137622(v=sql.105).aspx
    When I face issue i generally look at wait stats ,wait stats give you idea about on what resource query was waiting.
    --By Jonathan KehayiasSELECT TOP 10
    wait_type ,
    max_wait_time_ms wait_time_ms ,
    signal_wait_time_ms ,
    wait_time_ms - signal_wait_time_ms AS resource_wait_time_ms ,
    100.0 * wait_time_ms / SUM(wait_time_ms) OVER ( )
    AS percent_total_waits ,
    100.0 * signal_wait_time_ms / SUM(signal_wait_time_ms) OVER ( )
    AS percent_total_signal_waits ,
    100.0 * ( wait_time_ms - signal_wait_time_ms )
    / SUM(wait_time_ms) OVER ( ) AS percent_total_resource_waits
    FROM sys.dm_os_wait_stats
    WHERE wait_time_ms > 0 -- remove zero wait_time
    AND wait_type NOT IN -- filter out additional irrelevant waits
    ( 'SLEEP_TASK', 'BROKER_TASK_STOP', 'BROKER_TO_FLUSH',
    'SQLTRACE_BUFFER_FLUSH','CLR_AUTO_EVENT', 'CLR_MANUAL_EVENT',
    'LAZYWRITER_SLEEP', 'SLEEP_SYSTEMTASK', 'SLEEP_BPOOL_FLUSH',
    'BROKER_EVENTHANDLER', 'XE_DISPATCHER_WAIT', 'FT_IFTSHC_MUTEX',
    'CHECKPOINT_QUEUE', 'FT_IFTS_SCHEDULER_IDLE_WAIT',
    'BROKER_TRANSMITTER', 'FT_IFTSHC_MUTEX', 'KSOURCE_WAKEUP',
    'LAZYWRITER_SLEEP', 'LOGMGR_QUEUE', 'ONDEMAND_TASK_QUEUE',
    'REQUEST_FOR_DEADLOCK_SEARCH', 'XE_TIMER_EVENT', 'BAD_PAGE_PROCESS',
    'DBMIRROR_EVENTS_QUEUE', 'BROKER_RECEIVE_WAITFOR',
    'PREEMPTIVE_OS_GETPROCADDRESS', 'PREEMPTIVE_OS_AUTHENTICATIONOPS',
    'WAITFOR', 'DISPATCHER_QUEUE_SEMAPHORE', 'XE_DISPATCHER_JOIN',
    'RESOURCE_QUEUE' )
    ORDER BY wait_time_ms DESC
    use below link to analyze wait stats
    http://www.sqlskills.com/blogs/paul/wait-statistics-or-please-tell-me-where-it-hurts/
    HTH
    PS: for reporting services you can post in SSRS forum
    Please mark this reply as the answer or vote as helpful, as appropriate, to make it useful for other readers

  • Need help in improving the performance for the sql query

    Thanks in advance for helping me.
    I was trying to improve the performance of the below query. I tried the following methods used merge instead of update, used bulk collect / Forall update, used ordered hint, created a temp table and upadated the target table using the same. The methods which I used did not improve any performance. The data count which is updated in the target table is 2 million records and the target table has 15 million records.
    Any suggestions or solutions for improving performance are appreciated
    SQL query:
    update targettable tt
    set mnop = 'G',
    where ( x,y,z ) in
    select a.x, a.y,a.z
    from table1 a
    where (a.x, a.y,a.z) not in (
    select b.x,b.y,b.z
    from table2 b
    where 'O' = b.defg
    and mnop = 'P'
    and hijkl = 'UVW';

    987981 wrote:
    I was trying to improve the performance of the below query. I tried the following methods used merge instead of update, used bulk collect / Forall update, used ordered hint, created a temp table and upadated the target table using the same. The methods which I used did not improve any performance. And that meant what? Surely if you spend all that time and effort to try various approaches, it should mean something? Failures are as important teachers as successes. You need to learn from failures too. :-)
    The data count which is updated in the target table is 2 million records and the target table has 15 million records.Tables have rows btw, not records. Database people tend to get upset when rows are called records, as records exist in files and a database is not a mere collection of records and files.
    The failure to find a single faster method with the approaches you tried, points to that you do not know what the actual performance problem is. And without knowing the problem, you still went ahead, guns blazing.
    The very first step in dealing with any software engineering problem, is to identify the problem. Seeing the symptoms (slow performance) is still a long way from problem identification.
    Part of identifying the performance problem, is understanding the workload. Just what does the code task the database to do?
    From your comments, it needs to find 2 million rows from 15 million rows. Change these rows. And then write 2 million rows back to disk.
    That is not a small workload. Simple example. Let's say that the 2 million row find is 1ms/row and the 2 million row write is also 1ms/row. This means a 66 minute workload. Due to the number of rows, an increase in time/row either way, will potentially have 2 million fold impact.
    So where is the performance problem? Time spend finding the 2 million rows (where other tables need to be read, indexes used, etc)? Time spend writing the 2 million rows (where triggers and indexes need to be fired and maintained)? Both?

  • How to obtain the transformed SQL query using SEM_MATCH

    Dear all,
    Is it possible to get the transformed relational SQL query when using the SEM_MATCH prefix, by querying directly on the database. We are able to obtain the relational SQL query using Joseki/Jena, however this is not the way to go for us. We would like (if possible) to get it straight from the oracle database by logging or something.
    Kind regards.
    Max

    Hi Max,
    Just to clarify. What SEM_MATCH prefix are you talking about?
    A SEM_MATCH based query is indeed a SQL query as SEM_MATCH is a SQL table function. So if you don't want to go through Java APIs or web service endpoint, then running SEM_MATCH directly should give you what you need. Or maybe you just want to see the underlying generated (from SEM_MATCH) SQL query. If that is true, can you please tell us why?
    Thanks,
    Zhe Wu

  • Set TIMEOUT on OCCI sql query using ?

    Is there a way to set a timeout on a sql query using OCCI ?

    OCCI does not provide an API to set timeouts on SQL Queries.
    You can use the CREATE PROFILE command and assign resources at
    a user level. The restrictions may be applied to memory/resources which
    in turn would control the SQL execution time. Please check the
    CREATE PROFILE command.
    Rgds
    Amogh

  • Write the following query using JOIN keyword ?

    Qest:
    Write the following query using JOIN keyword ?
    select ID_category from category_master X
    where Exist(select 1 from sub_category Y where
    X.ID_category=Y.ID_category)Edited by: 799660 on Oct 3, 2010 6:05 AM

    select  X.ID_category
      from  category_master X join (select distinct ID_category from sub_category) Y
             on (X.ID_category=Y.ID_category)
    /SY.

  • How to find sql query using sqlid

    Hi
    I am not aware of that sqlid .DBA is saying a particular sqlid is making problem .how to find the particular sql query using sql_id ?

    Are you aware of modplsql when i executed the query the result is like
    DECLARE
       rc__             NUMBER;
       simple_list__    OWA_UTIL.vc_arr;
       complex_list__   OWA_UTIL.vc_arr;
    BEGIN
       OWA.init_cgi_env (:n__, :nm__, :v__);
       HTP.htbuf_len := 84;
       NULL;
       NULL;
       simple_list__ (1) := 'sys.%';
       simple_list__ (2) := 'dbms\_%';
       simple_list__ (3) := 'utl\_%';
       simple_list__ (4) := 'owa\_%';
       simple_list__ (5) := 'owa.%';
       simple_list__ (6) := 'htp.%';
       simple_list__ (7) := 'htf.%';
       simple_list__ (8) := 'wpg_docload.%';
       IF ((owa_match.match_pattern ('Oly_browse.oly_pattern ',
                                     simple_list__,
                                     complex_list__,
                                     TRUE
       THEN
          rc__ := 2;
       ELSE
          NULL;
          NULL;
         oly_browse.oly_pattern (search_phrase      => :search_phrase,           --Oly_browse.oly_pattern is package name
                                btn                => :btn,
                                p_qual             => :p_qual,
                                p_bcat             => :p_bcat,
                                p_stdy             => :p_stdy,
                                p_bloc             => :p_bloc,
                                z                  => :z
          IF (WPG_DOCLOAD.is_file_download)
          THEN
             rc__ := 1;
             WPG_DOCLOAD.get_download_file (:doc_info);
             NULL;
             NULL;
             NULL;
             COMMIT;
          ELSE
             rc__ := 0;
             NULL;
             NULL;
             NULL;
             COMMIT;
             OWA.get_page (:data__, :ndata__);
          END IF;
       END IF;
       :rc__ := rc__;
    END; Edited by: vishnu prakash on Sep 8, 2010 10:16 PM

  • Select Query using Joins

    Hi,
    Please give me the Select query
    I have to fetch   EBELN,BUKRS,LIFNR,BSART,EKORG,EKGRP,WAERS,WKURS  from EKKO and
                          EBELP,LGORT,MATKL ,WERKS,BEDNR,IDNLF,BPRME,NETPR   from EKPO and
                         MENGE, SAKTO,AUFNR,ANLN1,GSBER,PS_PSP_PNR                from EKKN and
                         LPEIN,EINDT,SLFDT,                                                                    from EKET.
    I have to fetch all the data into an single internal table(Mandatory) by a select query using Joins.
    EKKO is having EBELN as primary key
    EKPO is having EBELN,EBELP as primary key
    EKKN is having EBELN,EBELP as primary key
    EKET is having EBELN,EBELP as primary key
    Can anybody Please give me the Select query.
    With Regards
    Ajay

    Hi ,
    I am sending this sample coed for the Inner joins .Hope this will help you:
    Inner joins using 3 tables 
    Try this :-
    SELECT stpostlnr stpoidnrk mastmatnr maramtart stpo~menge 
    INTO CORRESPONDING FIELDS OF TABLE zmat1 FROM mast 
    JOIN stpo ON stpostlnr = maststlnr 
    JOIN mara ON maramatnr = mastmatnr 
    WHERE stpostlty = 'M' "AND stpoidnrk IN s_matnr 
    AND mast~werks = 1000. 
    Here s_matnr is a select-options on the selection-screen. 
    Or this. 
    Code: 
         Select single VbrkBukrs VbrkKunrg    Vbrk~Vbeln 
                       VbrkFkdat VbrkBstnk_Vf Vbrk~Zterm 
                       Tvzbt~Vtext 
                       VbakVbeln VbakBstdk 
                       LikpVbeln Likplfdat    Likp~Lfuhr 
           into w_vbrk 
           from vbrk 
          inner join       Tvzbt on TvzbtZterm        = VbrkZterm      and 
                                    Tvzbt~Spras        = sy-langu 
          Inner join       Vbfa  as SalesLnk 
                                 on SalesLnk~vbeln     = pu_vbeln        and 
                                    SalesLnk~vbtyp_v   = c_order 
                inner join Vbak  on VbakVbeln           = SalesLnkVbelv
          Inner join       Vbfa  as DeliveryLnk 
                                 on DeliveryLnk~vbeln   = pu_vbeln       and 
                                    DeliveryLnk~vbtyp_v = c_Delivery 
                inner join Likp  on LikpVbeln          = DeliveryLnkVbelv 
          where vbrk~vbeln = pu_Vbeln. 
    This code locates sales, delivery and payment terms info from a billing document number. 
    or
    Here, this one also works fine :
    select zfpcdcadivi zfpcdproforma zfpcdfactura zfpcdaniofactura 
    zfpcdmontousd zfpcdmontoap zfpcdebeln zfpcdinco1 
    zfpcdlifnr lfa1name1 zcdvsstatus zfpcdconint 
    into it_lista 
    from zfpcd inner join zcdvs 
    on zfpcdebeln = zcdvsebeln 
    and zfpcdproforma = zcdvsproforma 
    and zfpcdlifnr = zcdvslifnr 
    inner join lfa1 
    on zfpcdlifnr = lfa1lifnr 
    where zcdvs~status = '04'. 
    Also Here is another solution that just uses inner joins:
    SELECT vbakvbeln vbfaerdat INTO (itab-vbeln, itab-wadat)
    FROM ( vbak INNER JOIN vbap
    ON vbapvbeln = vbakvbeln )
    INNER JOIN vbfa ON vbakvbeln = vbfavbelv
    WHERE vbak~kunnr = m_wm AND
    vbak~vbtyp = 'C' AND
    vbfa~erdat IN s_date AND
    vbap~matnr = 'MZ1807F' AND
    vbfaposnv = vbapposnr AND
    vbfa~vbtyp_n = 'J'.
    <REMOVED BY MODERATOR>
    Cheers,
    Chandra Sekhar.
    Edited by: Alvaro Tejada Galindo on Apr 10, 2008 5:20 PM

  • Modify query used in Materialized View

    Are there any ways to modify the query used in materialized view without dropping the materialized view. I dont think alter materialized view has such an option.
    Please provider me your suggestions.

    you have to give "ENABLE QUERY REWRITE" option then u can change your query

  • Tuning an SQL query on a view

    Hi
    I have an SQL that has poor performance when querying a 3 table view.
    I have tried to tune the view by adding an additional index but the EXPLAIN plan does not change.  In fact, the plan is ONLY using fields specified in the the views join conditions and not the WHERE clause.
    I have created an index EKET~Z1 with the column for table (T003)
          EINDT   
    and I have also tried another index EKKO~Z2 with the columns for table T001
          BSART
          BSTYP
          EKORG
    The query is as follows:
    SQL Statement                                                                               
    SELECT                                                                    
        "MANDT" , "EBELN" , "EBELP" , "ETENR" , "EKKO_LOEKZ" , "BSTYP" ,        
        "BSART" , "BUKRS" , "EKORG" , "EKGRP" , "STATU" , "AEDAT" , "ERNAM" ,   
        "BEDAT" , "LIFNR" , "ADRNR" , "WAERS" , "EKPO_LOEKZ" , "EKPO_BSTYP" ,   
        "TXZ01" , "MATNR" , "EMATN" , "WERKS" , "MATKL" , "IDNLF" , "MENGE" ,   
        "MEINS" , "NETPR" , "NETWR" , "PSTYP" , "KNTTP" , "WEPOS" , "ANFNR" ,   
        "PRDAT" , "KONNR" , "KTPNR" , "ELIKZ" , "EREKZ" , "EINDT" , "SLFDT" ,   
        "LPEIN" , "EKET_MENGE" , "EKET_WEMNG" , "BANFN" , "BNFPO" , "MBDAT" ,   
        "WADAT" , "WEBRE" , "FRGRL" , "ZZKONNR" , "ZZKTPNR" , "MAHNZ" ,         
        "MAHN1" , "MAHN2" , "MAHN3" , "INFNR"                                   
      FROM                                                                      
        "ZPO_DETAIL"                                                            
      WHERE                                                                     
        "MANDT" = ? AND "EKORG" = ? AND "BSTYP" = ? AND "BSART" = ? AND         
        "WERKS" = ? AND "EINDT" < ? AND "EKPO_LOEKZ" <> ? AND "ELIKZ" <> ?      
      ORDER BY                                                                  
        "EBELN" , "EBELP" , "ETENR"                                             
    VIEW DETAILS
    CREATE VIEW ZPO_DETAIL          
       (MANDT,                      
       EBELN,                       
       EBELP,                       
       ETENR,                       
       EKKO_LOEKZ,                  
       BSTYP,                       
       BSART,                       
       BUKRS,                       
       EKORG,                       
       EKGRP,                       
       STATU,                       
       AEDAT,                       
       ERNAM,                       
       BEDAT,                       
       LIFNR,                       
       ADRNR,                       
       WAERS,                       
       EKPO_LOEKZ,                  
       EKPO_BSTYP,                  
       TXZ01,                       
       MATNR,                       
       EMATN,                       
    WERKS,              
    MATKL,              
    IDNLF,              
    MENGE,              
    MEINS,              
    NETPR,              
    NETWR,              
    PSTYP,              
    KNTTP,              
    WEPOS,              
    ANFNR,              
    PRDAT,              
    KONNR,              
    KTPNR,              
    ELIKZ,              
    EREKZ,              
    EINDT,              
    SLFDT,              
    LPEIN,              
    EKET_MENGE,         
    EKET_WEMNG,         
    BANFN,              
    BNFPO,              
       MBDAT,        
       WADAT,        
       WEBRE,        
       FRGRL,        
       ZZKONNR,      
       ZZKTPNR,      
       MAHNZ,        
       MAHN1,        
       MAHN2,        
       MAHN3 )       
    AS SELECT        
       T0003."MANDT",
        T0003."EBELN",
        T0003."EBELP",
        T0001."ETENR",
        T0002."LOEKZ",
        T0002."BSTYP",
        T0002."BSART",
        T0002."BUKRS",
        T0002."EKORG",
        T0002."EKGRP",
        T0002."STATU",
        T0002."AEDAT",
       T0002."ERNAM",
       T0002."BEDAT",
       T0002."LIFNR",
       T0002."ADRNR",
       T0002."WAERS",
       T0003."LOEKZ",
       T0003."BSTYP",
       T0003."TXZ01",
       T0003."MATNR",
       T0003."EMATN",
       T0003."WERKS",
       T0003."MATKL",
       T0003."IDNLF",
       T0003."MENGE",
       T0003."MEINS",
       T0003."NETPR",
       T0003."NETWR",
       T0003."PSTYP",
       T0003."KNTTP",
       T0003."WEPOS",
       T0003."ANFNR",
       T0003."PRDAT",
       T0003."KONNR",
        T0003."KTPNR",         
        T0003."ELIKZ",         
        T0003."EREKZ",         
        T0001."EINDT",         
        T0001."SLFDT",         
        T0001."LPEIN",         
        T0001."MENGE",         
        T0001."WEMNG",         
        T0001."BANFN",         
        T0001."BNFPO",         
        T0001."MBDAT",         
        T0001."WADAT",         
        T0003."WEBRE",         
        T0002."FRGRL",         
        T0003."ZZKONNR",       
        T0003."ZZKTPNR",       
        T0003."MAHNZ",         
        T0003."MAHN1",         
        T0003."MAHN2",         
        T0003."MAHN3"          
    FROM                       
       SAPCEP."EKET" T0001,    
        SAPCEP."EKKO" T0002,   
        SAPCEP."EKPO" T0003              
    WHERE                                
        T0002."MANDT" = T0003."MANDT" AND
        T0002."EBELN" = T0003."EBELN" AND
        T0003."MANDT" = T0001."MANDT" AND
        T0003."EBELN" = T0001."EBELN" AND
        T0003."EBELP" = T0001."EBELP"    
    EXPLAIN PLAN
    Execution Plan for SQL Optimizer
                                                                                    TABLENAME     COLUMN OR INDEX                STRATEGY                                     PAGECOUNT                                                                               
    T0003                                                             RANGE CONDITION FOR KEY         15758    
                             MANDT                                         (USED KEY COLUMN)   
    T0001                                                             JOIN VIA MULTIPLE KEY COLUMNS  8632    
                             MANDT                                          (USED KEY COLUMN)                                 
                             EBELN                                           (USED KEY COLUMN)    
    T0002                                                             JOIN VIA MULTIPLE KEY COLUMNS   40109    
                             MANDT                                          (USED KEY COLUMN)                                 
                             EBELN                                           (USED KEY COLUMN)                                 
                             EBELP                                           (USED KEY COLUMN)                                                                               
    NO TEMPORARY RESULTS CREATED                   
    SHOW                                                               RESULT IS COPIED   , COSTVALUE IS   31186  
    SHOW                                                          QUERYREWRITE - APPLIED RULES:                          
                       SHOW                                                             MergeFromSelectOrView                          1                                                                               
    The statistics are up-to-date.
    Any tips would be welcome.
    Thanks
    Doug

    > I have created an index EKET~Z1 with the column for table (T003)
    >       EINDT   
    >
    > and I have also tried another index EKKO~Z2 with the columns for table T001
    >       BSART
    >       BSTYP
    >       EKORG
    >
    SQL Statement                                                               
    >                                                                             
    >   SELECT                                                                    
    >     "MANDT" , "EBELN" , "EBELP" , "ETENR" , "EKKO_LOEKZ" , "BSTYP" ,        
    >   FROM                                                                      
    >     "ZPO_DETAIL"                                                            
    >   WHERE                                                                     
    >     "MANDT" = ? AND "EKORG" = ? AND "BSTYP" = ? AND "BSART" = ? AND         
    >     "WERKS" = ? AND "EINDT" < ? AND "EKPO_LOEKZ" <> ? AND "ELIKZ" <> ?      
    >   ORDER BY                                                                  
    >     "EBELN" , "EBELP" , "ETENR"                                             
    >
    > EXPLAIN PLAN
    > -
    >
    > Execution Plan for SQL Optimizer
    >                                                                               
    > TABLENAME     COLUMN OR INDEX                STRATEGY                                     PAGECOUNT     
    >                                                                               
    > T0003                                                             RANGE CONDITION FOR KEY         15758    
    >                          MANDT                                         (USED KEY COLUMN)   
    >                              
    > T0001                                                             JOIN VIA MULTIPLE KEY COLUMNS  8632    
    >                          MANDT                                          (USED KEY COLUMN)                                 
    >                          EBELN                                           (USED KEY COLUMN)    
    >                             
    > T0002                                                             JOIN VIA MULTIPLE KEY COLUMNS   40109    
    >                          MANDT                                          (USED KEY COLUMN)                                 
    >                          EBELN                                           (USED KEY COLUMN)                                 
    >                          EBELP                                           (USED KEY COLUMN)     
    >                            
    >                                                                          NO TEMPORARY RESULTS CREATED                   
    > SHOW                                                               RESULT IS COPIED   , COSTVALUE IS   31186  
    > SHOW                                                          QUERYREWRITE - APPLIED RULES:                          
    >                    SHOW                                                             MergeFromSelectOrView                          1    
    >                                                                               
    >
    > The statistics are up-to-date.
    > Any tips would be welcome.
    Hi Doug,
    here we go...
    What MaxDB version are you using? Did you check whether the DB parameters all comply to the recommendations?
    If so, what are the cardinalities of the columns you indexed (a.k.a. how many different values do each of them contain?)
    If the specification of BSART,  BSTYP and  EKORG does not reduce the number of rows to be retrieved to a large extent, then the indexaccess is likely to produce additional work.
    Anyhow, the join order looks a bit odd here - this might be the problem caused by having set OPTIMIZE_OPERATOR_JOIN_COSTFUNC = YES in a MaxDB version between 7.6.04 and 7.6.06.
    So make sure it's set to NO (see note 814704 MaxDB Version 7.6 parameter settings for OLTP/BW).
    regards,
    Lars

  • Sql query using LIKE is very slow

    Hi,
    I am running SQL query with LIKE on two table with more than two million records on oracle 10g and sun solaris OS. Does anybody have any idea or alternative to improve this query?
    it will never use index because i m using LIKE '%xyz%'. I Have posted query below
    PROCEDURE order_search
    v_search_type_in IN VARCHAR2
    ,v_search_value1_in IN VARCHAR2 DEFAULT 'NONE'
    ,v_search_value2_in IN VARCHAR2 DEFAULT 'NONE'
    ,v_group_id_in IN bcf_groups.group_id%TYPE DEFAULT 0
    ,v_open_in IN NUMBER DEFAULT 0
    ,v_requested_in IN NUMBER DEFAULT 0
    ,v_cancelled_in IN NUMBER DEFAULT 0
    ,v_closed_in IN NUMBER DEFAULT 0
    ,v_employee_id_in IN sxweb00.customer.customer_id%TYPE DEFAULT 0
    ,outcursor IN OUT FulfillmentCurType
    IS
    v_status_code NUMBER;
    v_upper_search_value1 VARCHAR2(500);
    v_lower_search_value1 VARCHAR2(500);
    v_open_status VARCHAR2(10);
    v_closed_status VARCHAR2(10);
    v_cancelled_status VARCHAR2(10);
    v_requested_status VARCHAR2(10);
    v_group_for_search bcf_groups.group_id%TYPE;
    v_sql_select VARCHAR2(4000);
    v_sql_from VARCHAR2(4000);
    v_sql_where VARCHAR2(4000);
    v_sql_order_by VARCHAR2(4000);
    v_group_where VARCHAR2(100);
    v_status_where VARCHAR2(500);
    BEGIN
    IF v_open_in = 1 THEN
    v_open_status := 'OPEN';
    END IF;
    IF v_closed_in = 1 THEN
    v_closed_status := 'CLOSED';
    END IF;
    IF v_cancelled_in = 1 THEN
    v_cancelled_status := 'CANCELLED';
    END IF;
    IF v_requested_in = 1 THEN
    v_requested_status := 'REQUESTED';
    END IF;
    IF UPPER(v_search_type_in) = 'GROUP'
    THEN
    v_group_for_search := v_search_value1_in;
    ELSE
    v_group_for_search := v_group_id_in;
    END IF;
    -- This is the select statement used for all search types
    v_sql_select := ' '
    || 'po.order_id order_id '
    || ',decode('||v_group_id_in||',4,substr(bcf_locator.get_first_location(po.order_id,'||v_group_id_in||'),3),null) first_location '
    || ',TO_CHAR(po.order_date, ''mm/dd/yyyy'') order_date '
    || ',' || v_group_for_search || ' group_id '
    || ',TRIM(TO_CHAR(bcf_fulfillment.get_shipping_total(po.order_id,' || v_group_for_search || ') + bcf_fulfillment.get_tax_total(po.order_id,' || v_group_for_search || ') + bcf_fulfillment.get_subtotal(po.order_id,' || v_group_for_search || ') ,''$999,999,999,999,999,990.99'')) total '
    || ',bcf_fulfillment.get_billing_name (po.customer_id) billing_name '
    || ',bcf_fulfillment.get_open_and_assigned_count(po.order_id,' || v_group_for_search || ') open_and_assigned_count '
    || ',bcf_fulfillment.get_divisions(po.order_id,' || v_group_for_search || ') divisions '
    || ',bcf_fulfillment.get_division_count (po.order_id,' || v_group_for_search || ' ) division_count '
    || ',bcf_fulfillment.get_picker_list (po.order_id,' || v_group_for_search || ') picker_name '
    || ',bcf_fulfillment.get_picker_count (po.order_id,' || v_group_for_search || ') picker_count '
    || ',bcf_fulfillment.get_order_status(po.order_id,' || v_group_for_search || ') order_status '
    || ',po.customer_id customer_id '
    -- IF there is only one unique picker then that means there are no unassigned or NULL pickers
    -- therefore by default the checkbox should not be selected else the count picker_count is
    -- 0 or > 1 meaning that at least one line item has an unassigned employee_id and by default
    -- the checkbox should be selected
    || ',DECODE(bcf_fulfillment.get_picker_count(po.order_id,' || v_group_for_search || '),1 ,0,1) print_cb '
    || ',bcf_fulfillment.get_fulfillment_codes(po.order_id,' || v_group_for_search || ') fulfillment_types '
    || ',bcf_fulfillment.has_group_been_reassigned(po.order_id,' || v_group_for_search || ') group_reassigned_flag '
    || ',bcf_fulfillment.get_chain_codes(po.order_id,' || v_group_for_search || ') chain_codes '
    || ',po.status_id order_status_id '
    || ',bcf_fulfillment.get_viewed_by_picker_flag(po.order_id,' || v_group_for_search || ') viewed_by_picker_flag '
    || ',bcf_fulfillment.get_picker_list (po.order_id,' || v_group_for_search || ', ''N'') pickers_without_unassigned '
    || ',bcf_fulfillment.get_group_list (po.order_id) group_list '
    || ',bcf_fulfillment.get_line_item_count(po.order_id,' || v_group_for_search || ') line_item_count '
    || ',bcf_fulfillment.get_shipping_priority(po.order_id,' || v_group_for_search || ') shipping_priority'
    -- Anna wants all users to know and always wants to know no matter what the status of the auth or order
    || ',(SELECT decode(count(*),0,''N'',''Y'') FROM bcf_payment WHERE ics_rflag = ''DAVSNO'' AND order_id = po.order_id AND status_code IN (''ATH2'',''FUL2'',''CPT0'',''CPT2'')) avs_failed'
    || ',trunc(po.order_date) date_for_sort'
    -- When we need to look at all orders then the v_group_for_search will be zero i.e. customer service rep
    IF v_group_for_search = 0 THEN
    -- This is the core tables used by most search types
    v_sql_from := ' '
    || 'FROM product_order po'
    -- || ',bcf_lookup lu'
    -- This is the core table join needed for most search types
    v_sql_where := ' '
    || 'WHERE 1 = 1 '
    -- No orders will be looked at for fulfillment purposes that occurred before the minimum
    -- shipping cost functionality was added
    -- || 'AND po.order_date >= lu.implement_date '
    || 'AND po.order_date >= (SELECT min(implement_date) FROM bcf_lookup) '
    || 'AND po.submitted_flag = 1 '
    || 'AND po.channel_code = ''BCF'' '
    -- When we are looking at all orders then we are not going to restrict it by group
    v_group_where := ' ';
    -- This is used where the search type requires searching by the Open, Closed, and Cancelled status.
    -- We are binding the three status variables so that they can be cached in Oracle and not need to be parsed after
    -- the first search of a search type is done.
    v_status_where := ' '
    || 'AND bcf_fulfillment.get_order_status(po.order_id,' || v_group_for_search|| ') '
    || 'IN (:v_open_status, :v_requested_status, :v_cancelled_status, :v_closed_status) '
    ELSE
    -- This is the core tables used by most search types
    v_sql_from := ' '
    || 'FROM product_order po'
    || ',bcf_product_order_groups pog '
    -- This is the core table join needed for most search types
    v_sql_where := ' '
    || 'WHERE 1 = 1 '
    || 'AND po.order_id = pog.order_id '
    || 'AND po.channel_code = ''BCF'' '
    -- This is used only where the search type requires searching by a group.
    v_group_where := ' '
    || 'AND pog.group_id = ' || v_group_for_search
    -- This is used where the search type requires searching by the Open, Closed, and Cancelled status.
    -- We are binding the three status variables so that they can be cached in Oracle and not need to be parsed after
    -- the first search of a search type is done.
    v_status_where := ' '
    || 'AND pog.dn_status IN (:v_open_status, :v_requested_status, :v_cancelled_status, :v_closed_status) '
    END IF;
    -- If an employee_id is provided then we need to add in a where clause so that only orders that belong to that
    -- employee are pulled
    IF v_employee_id_in > 0
    THEN
    v_sql_where := v_sql_where
    || 'AND pog.dn_employee_id = ' || TO_CHAR(v_employee_id_in) || ' '
    END IF;
    IF v_group_id_in = get_group_fulfillment_center1
    THEN
    v_sql_order_by := 'ORDER BY '
    || 'bcf_fulfillment.get_division_count (po.order_id,' || v_group_for_search || ') '
    || ',bcf_fulfillment.get_divisions(po.order_id,' || v_group_for_search || ') '
    || ',po.order_id '
    ELSIF v_group_id_in = c_GROUP_FULFILLMENT_CENTER_212
    THEN
    v_sql_order_by := 'ORDER BY '
    || 'bcf_fulfillment.get_shipping_priority(po.order_id,' || v_group_for_search || ') DESC'
    || ',TRUNC(po.order_date)'
    || ',bcf_fulfillment.get_line_item_count(po.order_id,' || v_group_for_search || ')'
    || ',decode('||v_group_id_in||',4,substr(bcf_locator.get_first_location(po.order_id,'||v_group_id_in||'),3),null)'
    || ',po.order_id'
    ELSE
    v_sql_order_by := 'ORDER BY '
    || ' bcf_fulfillment.get_shipping_priority(po.order_id,' || v_group_for_search || ') DESC'
    || ',chain_codes DESC'
    || ',po.order_id '
    END IF;
    IF UPPER(v_search_type_in) = 'ORDERNUMBER'
    THEN
    v_sql_where := v_sql_where || v_group_where
    || 'AND po.order_id = :v_search_value1_in '
    IF (outcursor%ISOPEN) THEN
    CLOSE outcursor;
    END IF;
    OPEN outcursor FOR
    'SELECT ' || v_sql_select || v_sql_from || v_sql_where
    USING v_search_value1_in;
    ELSIF UPPER(v_search_type_in) = 'STATUSONLY'
    THEN
    v_sql_where := v_sql_where || v_group_where || v_status_where
    IF (outcursor%ISOPEN) THEN
    CLOSE outcursor;
    END IF;
    OPEN outcursor FOR
    'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
    USING v_open_status, v_requested_status, v_cancelled_status, v_closed_status;
    ELSIF UPPER(v_search_type_in) = 'BILLINGNAME'
    THEN
    v_upper_search_value1 := UPPER(v_search_value1_in);
    v_sql_where := v_sql_where || ' ' || v_group_where
    || 'AND po.upper_billing_name LIKE ''%'' || :v_upper_search_value1 || ''%'' '
    IF (outcursor%ISOPEN) THEN
    CLOSE outcursor;
    END IF;
    OPEN outcursor FOR
    'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
    USING v_upper_search_value1;
    ELSIF UPPER(v_search_type_in) = 'GROUP'
    THEN
    IF v_search_value1_in = '0' -- Unassigned
    THEN
    v_sql_from := v_sql_from || ' ,product_order_detail_actv_v pod ';
    v_sql_where := v_sql_where || ' ' || v_status_where
    || 'AND po.order_id = pod.order_id '
    || 'AND pod.group_id IS NULL '
    IF (outcursor%ISOPEN) THEN
    CLOSE outcursor;
    END IF;
    OPEN outcursor FOR
    'SELECT DISTINCT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
    USING v_open_status, v_requested_status, v_cancelled_status, v_closed_status;
    ELSE
    v_sql_where := v_sql_where || ' ' || v_status_where
    || 'AND pog.group_id = :v_group_for_search '
    IF (outcursor%ISOPEN) THEN
    CLOSE outcursor;
    END IF;
    OPEN outcursor FOR
    'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
    USING v_open_status, v_requested_status, v_cancelled_status, v_closed_status, v_group_for_search;
    END IF;
    ELSIF UPPER(v_search_type_in) = 'SHIPPINGNAME'
    THEN
    v_upper_search_value1 := UPPER(v_search_value1_in);
    v_sql_where := v_sql_where || ' ' || v_group_where
    || 'AND po.upper_shipping_name LIKE ''%'' || :v_upper_search_value1 || ''%'' '
    IF (outcursor%ISOPEN) THEN
    CLOSE outcursor;
    END IF;
    OPEN outcursor FOR
    'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
    USING v_upper_search_value1;
    ELSIF UPPER(v_search_type_in) = 'USERNAME'
    THEN
    v_lower_search_value1 := LOWER(v_search_value1_in);
    v_sql_from := v_sql_from || ' ,sxweb01.customer c ';
    v_sql_where := v_sql_where || ' ' || v_group_where
    || 'AND po.customer_id = c.customer_id '
    || 'AND c.username LIKE ''%'' || :v_lower_search_value1 || ''%'' '
    IF (outcursor%ISOPEN) THEN
    CLOSE outcursor;
    END IF;
    OPEN outcursor FOR
    'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
    USING v_lower_search_value1;
    ELSIF UPPER(v_search_type_in) = 'BILLINGADDRESS1'
    THEN
    v_upper_search_value1 := UPPER(v_search_value1_in);
    v_sql_from := v_sql_from || ' ,order_payment op '
    || ' ,credit_card cc '
    || ' ,om_address oma '
    v_sql_where := v_sql_where || ' ' || v_group_where
    || 'AND po.order_id = op.business_object_id '
    || 'AND op.business_object_type = ''ORDR'' '
    || 'AND op.payment_method = ''CRCD'' '
    || 'AND op.payment_method_id = cc.credit_card_id '
    || 'AND cc.address_id = oma.address_id '
    || 'AND UPPER(oma.address1) LIKE ''%'' || :v_upper_search_value1 || ''%'' '
    IF (outcursor%ISOPEN) THEN
    CLOSE outcursor;
    END IF;
    OPEN outcursor FOR
    'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
    USING v_upper_search_value1;
    ELSIF UPPER(v_search_type_in) = 'SHIPPINGADDRESS1'
    THEN
    v_upper_search_value1 := UPPER(v_search_value1_in);
    v_sql_from := v_sql_from || ' ,shipping_info si '
    || ' ,om_address oma '
    v_sql_where := v_sql_where || ' ' || v_group_where
    || 'AND po.order_id = si.order_id '
    || 'AND si.ship_to_address_id = oma.address_id '
    || 'AND UPPER(oma.address1) LIKE ''%'' || :v_upper_search_value1 || ''%'' '
    IF (outcursor%ISOPEN) THEN
    CLOSE outcursor;
    END IF;
    OPEN outcursor FOR
    'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
    USING v_upper_search_value1;
    ELSIF UPPER(v_search_type_in) = 'BILLINGPHONE'
    THEN
    v_sql_from := v_sql_from || ' ,order_payment op '
    || ' ,credit_card cc '
    v_sql_where := v_sql_where || ' ' || v_group_where
    || 'AND po.order_id = op.business_object_id '
    || 'AND op.business_object_type = ''ORDR'' '
    || 'AND op.payment_method = ''CRCD'' '
    || 'AND op.payment_method_id = cc.credit_card_id '
    || 'AND cc.home_phone = :v_search_value1_in '
    IF (outcursor%ISOPEN) THEN
    CLOSE outcursor;
    END IF;
    OPEN outcursor FOR
    'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
    USING v_search_value1_in;
    ELSIF UPPER(v_search_type_in) = 'SHIPPINGPHONE'
    THEN
    v_sql_from := v_sql_from || ' ,shipping_info si '
    v_sql_where := v_sql_where || ' ' || v_group_where
    || 'AND po.order_id = si.order_id '
    || 'AND si.ship_phone = :v_search_value1_in '
    IF (outcursor%ISOPEN) THEN
    CLOSE outcursor;
    END IF;
    OPEN outcursor FOR
    'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
    USING v_search_value1_in;
    ELSIF UPPER(v_search_type_in) = 'CREDITCARDNUMBER'
    THEN
    v_sql_from := v_sql_from || ' ,order_payment op '
    || ' ,credit_card cc '
    v_sql_where := v_sql_where || ' ' || v_group_where
    || 'AND po.order_id = op.business_object_id '
    || 'AND op.business_object_type = ''ORDR'' '
    || 'AND op.payment_method = ''CRCD'' '
    || 'AND op.payment_method_id = cc.credit_card_id '
    || 'AND cc.credit_card_number = :v_search_value1_in '
    IF (outcursor%ISOPEN) THEN
    CLOSE outcursor;
    END IF;
    OPEN outcursor FOR
    'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
    USING v_search_value1_in;
    ELSIF UPPER(v_search_type_in) = 'PONUMBER'
    THEN
    v_sql_from := ' '
    || 'FROM ' || ' product_order_detail_actv_v pod ,' || SUBSTR(v_sql_from, 6);
    v_sql_where := v_sql_where || v_group_where
    || 'AND pod.po_no = :v_search_value1_in '
    || 'AND po.order_id = pod.order_id '
    IF (outcursor%ISOPEN) THEN
    CLOSE outcursor;
    END IF;
    OPEN outcursor FOR
    'SELECT /*+ ORDERED */ DISTINCT ' || v_sql_select || v_sql_from || v_sql_where
    USING v_search_value1_in;
    ELSIF UPPER(v_search_type_in) = 'USERASSIGNED'
    THEN
    IF v_search_value1_in = '0' -- 'Unassigned'
    THEN
    v_sql_where := v_sql_where || ' ' || v_group_where || ' ' || v_status_where
    || 'AND pog.unassigned_flag = ''Y'' '
    IF (outcursor%ISOPEN) THEN
    CLOSE outcursor;
    END IF;
    OPEN outcursor FOR
    'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
    USING v_open_status, v_requested_status, v_cancelled_status, v_closed_status;
    ELSE
    v_sql_where := v_sql_where || ' ' || v_group_where || ' ' || v_status_where
    || 'AND pog.dn_employee_id = :v_search_value1_in '
    IF (outcursor%ISOPEN) THEN
    CLOSE outcursor;
    END IF;
    OPEN outcursor FOR
    'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
    USING v_open_status, v_requested_status, v_cancelled_status, v_closed_status, v_search_value1_in;
    END IF;
    ELSIF UPPER(v_search_type_in) = 'ORDERDATE'
    THEN
    v_sql_where := v_sql_where || ' ' || v_group_where || ' ' || v_status_where
    || 'AND po.order_date BETWEEN TO_DATE( :v_search_value1_in, ''MON DD YYYY HH24:MI:SS'') AND TO_DATE( :v_search_value2_in ,''MON DD YYYY HH24:MI:SS'') '
    IF (outcursor%ISOPEN) THEN
    CLOSE outcursor;
    END IF;
    OPEN outcursor FOR
    'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
    USING v_open_status, v_requested_status, v_cancelled_status, v_closed_status, v_search_value1_in, v_search_value2_in;
    ELSIF UPPER(v_search_type_in) = 'PRINTJOB'
    THEN
    IF (outcursor%ISOPEN) THEN
    CLOSE outcursor;
    END IF;
    OPEN outcursor FOR
    'SELECT ' || v_sql_select
    || ' '
    || 'FROM product_order po '
    || ' ,bcf_print_jobs pj '
    || 'WHERE 1 = 1 '
    || 'AND pj.order_id = po.order_id '
    || 'AND pj.username = :v_search_value1_in '
    || 'AND pj.create_date = TO_DATE( :v_search_value2_in,''MON DD YYYY HH24:MI:SS'') '
    || v_sql_order_by
    USING v_search_value1_in, v_search_value2_in;
    ELSIF UPPER(v_search_type_in) = 'FULFILLMENTTYPE'
    THEN
    v_sql_from := v_sql_from || ' ,product_order_detail_actv_v pod ';
    v_sql_where := v_sql_where || ' ' || v_group_where || ' ' || v_status_where
    || 'AND pod.order_id = po.order_id '
    -- Because we have to drill down the the POD level
    -- we now need to ensure the pod records match on group
    || 'AND pod.group_id = ' || v_group_for_search
    -- We curr. have 3 fufillment types FC165, FC212, FCBABY
    -- so substr on 1st char works for Garry
    || 'AND UPPER(SUBSTR(pod.fulfillment_type,1,1)) = UPPER(:v_search_value1_in) '
    IF (outcursor%ISOPEN) THEN
    CLOSE outcursor;
    END IF;
    OPEN outcursor FOR
    'SELECT DISTINCT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
    USING v_open_status, v_requested_status, v_cancelled_status, v_closed_status, v_search_value1_in;
    ELSIF UPPER(v_search_type_in) = 'INSUFFICIENTAUTHORIZATION'
    THEN
    v_sql_where := v_sql_where
    || 'AND bcf_get_authorization_amount(po.order_id) > 0'
    IF (outcursor%ISOPEN) THEN
    CLOSE outcursor;
    END IF;
    OPEN outcursor FOR
    'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by;
    END IF;
    END order_search;
    Thanks
    V
    Edited by: vishal patel on Oct 23, 2008 3:59 PM

    PROCEDURE order_search
    v_search_type_in IN VARCHAR2
    ,v_search_value1_in IN VARCHAR2 DEFAULT 'NONE'
    ,v_search_value2_in IN VARCHAR2 DEFAULT 'NONE'
    ,v_group_id_in IN bcf_groups.group_id%TYPE DEFAULT 0
    ,v_open_in IN NUMBER DEFAULT 0
    ,v_requested_in IN NUMBER DEFAULT 0
    ,v_cancelled_in IN NUMBER DEFAULT 0
    ,v_closed_in IN NUMBER DEFAULT 0
    ,v_employee_id_in IN sxweb00.customer.customer_id%TYPE DEFAULT 0
    ,outcursor IN OUT FulfillmentCurType
    IS
    v_status_code NUMBER;
    v_upper_search_value1 VARCHAR2(500);
    v_lower_search_value1 VARCHAR2(500);
    v_open_status VARCHAR2(10);
    v_closed_status VARCHAR2(10);
    v_cancelled_status VARCHAR2(10);
    v_requested_status VARCHAR2(10);
    v_group_for_search bcf_groups.group_id%TYPE;
    v_sql_select VARCHAR2(4000);
    v_sql_from VARCHAR2(4000);
    v_sql_where VARCHAR2(4000);
    v_sql_order_by VARCHAR2(4000);
    v_group_where VARCHAR2(100);
    v_status_where VARCHAR2(500);
    BEGIN
    IF v_open_in = 1 THEN
    v_open_status := 'OPEN';
    END IF;
    IF v_closed_in = 1 THEN
    v_closed_status := 'CLOSED';
    END IF;
    IF v_cancelled_in = 1 THEN
    v_cancelled_status := 'CANCELLED';
    END IF;
    IF v_requested_in = 1 THEN
    v_requested_status := 'REQUESTED';
    END IF;
    IF UPPER(v_search_type_in) = 'GROUP'
    THEN
    v_group_for_search := v_search_value1_in;
    ELSE
    v_group_for_search := v_group_id_in;
    END IF;
    -- This is the select statement used for all search types
    v_sql_select := ' '
    || 'po.order_id order_id '
    || ',decode('||v_group_id_in||',4,substr(bcf_locator.get_first_location(po.order_id,'||v_group_id_in||'),3),null) first_location '
    || ',TO_CHAR(po.order_date, ''mm/dd/yyyy'') order_date '
    || ',' || v_group_for_search || ' group_id '
    || ',TRIM(TO_CHAR(bcf_fulfillment.get_shipping_total(po.order_id,' || v_group_for_search || ') + bcf_fulfillment.get_tax_total(po.order_id,' || v_group_for_search || ') + bcf_fulfillment.get_subtotal(po.order_id,' || v_group_for_search || ') ,''$999,999,999,999,999,990.99'')) total '
    || ',bcf_fulfillment.get_billing_name (po.customer_id) billing_name '
    || ',bcf_fulfillment.get_open_and_assigned_count(po.order_id,' || v_group_for_search || ') open_and_assigned_count '
    || ',bcf_fulfillment.get_divisions(po.order_id,' || v_group_for_search || ') divisions '
    || ',bcf_fulfillment.get_division_count (po.order_id,' || v_group_for_search || ' ) division_count '
    || ',bcf_fulfillment.get_picker_list (po.order_id,' || v_group_for_search || ') picker_name '
    || ',bcf_fulfillment.get_picker_count (po.order_id,' || v_group_for_search || ') picker_count '
    || ',bcf_fulfillment.get_order_status(po.order_id,' || v_group_for_search || ') order_status '
    || ',po.customer_id customer_id '
    -- IF there is only one unique picker then that means there are no unassigned or NULL pickers
    -- therefore by default the checkbox should not be selected else the count picker_count is
    -- 0 or > 1 meaning that at least one line item has an unassigned employee_id and by default
    -- the checkbox should be selected
    || ',DECODE(bcf_fulfillment.get_picker_count(po.order_id,' || v_group_for_search || '),1 ,0,1) print_cb '
    || ',bcf_fulfillment.get_fulfillment_codes(po.order_id,' || v_group_for_search || ') fulfillment_types '
    || ',bcf_fulfillment.has_group_been_reassigned(po.order_id,' || v_group_for_search || ') group_reassigned_flag '
    || ',bcf_fulfillment.get_chain_codes(po.order_id,' || v_group_for_search || ') chain_codes '
    || ',po.status_id order_status_id '
    || ',bcf_fulfillment.get_viewed_by_picker_flag(po.order_id,' || v_group_for_search || ') viewed_by_picker_flag '
    || ',bcf_fulfillment.get_picker_list (po.order_id,' || v_group_for_search || ', ''N'') pickers_without_unassigned '
    || ',bcf_fulfillment.get_group_list (po.order_id) group_list '
    || ',bcf_fulfillment.get_line_item_count(po.order_id,' || v_group_for_search || ') line_item_count '
    || ',bcf_fulfillment.get_shipping_priority(po.order_id,' || v_group_for_search || ') shipping_priority'
    -- Anna wants all users to know and always wants to know no matter what the status of the auth or order
    || ',(SELECT decode(count(*),0,''N'',''Y'') FROM bcf_payment WHERE ics_rflag = ''DAVSNO'' AND order_id = po.order_id AND status_code IN (''ATH2'',''FUL2'',''CPT0'',''CPT2'')) avs_failed'
    || ',trunc(po.order_date) date_for_sort'
    -- When we need to look at all orders then the v_group_for_search will be zero i.e. customer service rep
    IF v_group_for_search = 0 THEN
    -- This is the core tables used by most search types
    v_sql_from := ' '
    || 'FROM product_order po'
    -- || ',bcf_lookup lu'
    -- This is the core table join needed for most search types
    v_sql_where := ' '
    || 'WHERE 1 = 1 '
    -- No orders will be looked at for fulfillment purposes that occurred before the minimum
    -- shipping cost functionality was added
    -- || 'AND po.order_date >= lu.implement_date '
    || 'AND po.order_date >= (SELECT min(implement_date) FROM bcf_lookup) '
    || 'AND po.submitted_flag = 1 '
    || 'AND po.channel_code = ''BCF'' '
    -- When we are looking at all orders then we are not going to restrict it by group
    v_group_where := ' ';
    -- This is used where the search type requires searching by the Open, Closed, and Cancelled status.
    -- We are binding the three status variables so that they can be cached in Oracle and not need to be parsed after
    -- the first search of a search type is done.
    v_status_where := ' '
    || 'AND bcf_fulfillment.get_order_status(po.order_id,' || v_group_for_search|| ') '
    || 'IN (:v_open_status, :v_requested_status, :v_cancelled_status, :v_closed_status) '
    ELSE
    -- This is the core tables used by most search types
    v_sql_from := ' '
    || 'FROM product_order po'
    || ',bcf_product_order_groups pog '
    -- This is the core table join needed for most search types
    v_sql_where := ' '
    || 'WHERE 1 = 1 '
    || 'AND po.order_id = pog.order_id '
    || 'AND po.channel_code = ''BCF'' '
    -- This is used only where the search type requires searching by a group.
    v_group_where := ' '
    || 'AND pog.group_id = ' || v_group_for_search
    -- This is used where the search type requires searching by the Open, Closed, and Cancelled status.
    -- We are binding the three status variables so that they can be cached in Oracle and not need to be parsed after
    -- the first search of a search type is done.
    v_status_where := ' '
    || 'AND pog.dn_status IN (:v_open_status, :v_requested_status, :v_cancelled_status, :v_closed_status) '
    END IF;
    -- If an employee_id is provided then we need to add in a where clause so that only orders that belong to that
    -- employee are pulled
    IF v_employee_id_in > 0
    THEN
    v_sql_where := v_sql_where
    || 'AND pog.dn_employee_id = ' || TO_CHAR(v_employee_id_in) || ' '
    END IF;
    IF v_group_id_in = get_group_fulfillment_center1
    THEN
    v_sql_order_by := 'ORDER BY '
    || 'bcf_fulfillment.get_division_count (po.order_id,' || v_group_for_search || ') '
    || ',bcf_fulfillment.get_divisions(po.order_id,' || v_group_for_search || ') '
    || ',po.order_id '
    ELSIF v_group_id_in = c_GROUP_FULFILLMENT_CENTER_212
    THEN
    v_sql_order_by := 'ORDER BY '
    || 'bcf_fulfillment.get_shipping_priority(po.order_id,' || v_group_for_search || ') DESC'
    || ',TRUNC(po.order_date)'
    || ',bcf_fulfillment.get_line_item_count(po.order_id,' || v_group_for_search || ')'
    || ',decode('||v_group_id_in||',4,substr(bcf_locator.get_first_location(po.order_id,'||v_group_id_in||'),3),null)'
    || ',po.order_id'
    ELSE
    v_sql_order_by := 'ORDER BY '
    || ' bcf_fulfillment.get_shipping_priority(po.order_id,' || v_group_for_search || ') DESC'
    || ',chain_codes DESC'
    || ',po.order_id '
    END IF;
    IF UPPER(v_search_type_in) = 'ORDERNUMBER'
    THEN
    v_sql_where := v_sql_where || v_group_where
    || 'AND po.order_id = :v_search_value1_in '
    IF (outcursor%ISOPEN) THEN
    CLOSE outcursor;
    END IF;
    OPEN outcursor FOR
    'SELECT ' || v_sql_select || v_sql_from || v_sql_where
    USING v_search_value1_in;
    ELSIF UPPER(v_search_type_in) = 'STATUSONLY'
    THEN
    v_sql_where := v_sql_where || v_group_where || v_status_where
    IF (outcursor%ISOPEN) THEN
    CLOSE outcursor;
    END IF;
    OPEN outcursor FOR
    'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
    USING v_open_status, v_requested_status, v_cancelled_status, v_closed_status;
    ELSIF UPPER(v_search_type_in) = 'BILLINGNAME'
    THEN
    v_upper_search_value1 := UPPER(v_search_value1_in);
    v_sql_where := v_sql_where || ' ' || v_group_where
    || 'AND po.upper_billing_name LIKE ''%'' || :v_upper_search_value1 || ''%'' '
    IF (outcursor%ISOPEN) THEN
    CLOSE outcursor;
    END IF;
    OPEN outcursor FOR
    'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
    USING v_upper_search_value1;
    ELSIF UPPER(v_search_type_in) = 'GROUP'
    THEN
    IF v_search_value1_in = '0' -- Unassigned
    THEN
    v_sql_from := v_sql_from || ' ,product_order_detail_actv_v pod ';
    v_sql_where := v_sql_where || ' ' || v_status_where
    || 'AND po.order_id = pod.order_id '
    || 'AND pod.group_id IS NULL '
    IF (outcursor%ISOPEN) THEN
    CLOSE outcursor;
    END IF;
    OPEN outcursor FOR
    'SELECT DISTINCT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
    USING v_open_status, v_requested_status, v_cancelled_status, v_closed_status;
    ELSE
    v_sql_where := v_sql_where || ' ' || v_status_where
    || 'AND pog.group_id = :v_group_for_search '
    IF (outcursor%ISOPEN) THEN
    CLOSE outcursor;
    END IF;
    OPEN outcursor FOR
    'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
    USING v_open_status, v_requested_status, v_cancelled_status, v_closed_status, v_group_for_search;
    END IF;
    ELSIF UPPER(v_search_type_in) = 'SHIPPINGNAME'
    THEN
    v_upper_search_value1 := UPPER(v_search_value1_in);
    v_sql_where := v_sql_where || ' ' || v_group_where
    || 'AND po.upper_shipping_name LIKE ''%'' || :v_upper_search_value1 || ''%'' '
    IF (outcursor%ISOPEN) THEN
    CLOSE outcursor;
    END IF;
    OPEN outcursor FOR
    'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
    USING v_upper_search_value1;
    ELSIF UPPER(v_search_type_in) = 'USERNAME'
    THEN
    v_lower_search_value1 := LOWER(v_search_value1_in);
    v_sql_from := v_sql_from || ' ,sxweb01.customer c ';
    v_sql_where := v_sql_where || ' ' || v_group_where
    || 'AND po.customer_id = c.customer_id '
    || 'AND c.username LIKE ''%'' || :v_lower_search_value1 || ''%'' '
    IF (outcursor%ISOPEN) THEN
    CLOSE outcursor;
    END IF;
    OPEN outcursor FOR
    'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
    USING v_lower_search_value1;
    ELSIF UPPER(v_search_type_in) = 'BILLINGADDRESS1'
    THEN
    v_upper_search_value1 := UPPER(v_search_value1_in);
    v_sql_from := v_sql_from || ' ,order_payment op '
    || ' ,credit_card cc '
    || ' ,om_address oma '
    v_sql_where := v_sql_where || ' ' || v_group_where
    || 'AND po.order_id = op.business_object_id '
    || 'AND op.business_object_type = ''ORDR'' '
    || 'AND op.payment_method = ''CRCD'' '
    || 'AND op.payment_method_id = cc.credit_card_id '
    || 'AND cc.address_id = oma.address_id '
    || 'AND UPPER(oma.address1) LIKE ''%'' || :v_upper_search_value1 || ''%'' '
    IF (outcursor%ISOPEN) THEN
    CLOSE outcursor;
    END IF;
    OPEN outcursor FOR
    'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
    USING v_upper_search_value1;
    ELSIF UPPER(v_search_type_in) = 'SHIPPINGADDRESS1'
    THEN
    v_upper_search_value1 := UPPER(v_search_value1_in);
    v_sql_from := v_sql_from || ' ,shipping_info si '
    || ' ,om_address oma '
    v_sql_where := v_sql_where || ' ' || v_group_where
    || 'AND po.order_id = si.order_id '
    || 'AND si.ship_to_address_id = oma.address_id '
    || 'AND UPPER(oma.address1) LIKE ''%'' || :v_upper_search_value1 || ''%'' '
    IF (outcursor%ISOPEN) THEN
    CLOSE outcursor;
    END IF;
    OPEN outcursor FOR
    'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
    USING v_upper_search_value1;
    ELSIF UPPER(v_search_type_in) = 'BILLINGPHONE'
    THEN
    v_sql_from := v_sql_from || ' ,order_payment op '
    || ' ,credit_card cc '
    v_sql_where := v_sql_where || ' ' || v_group_where
    || 'AND po.order_id = op.business_object_id '
    || 'AND op.business_object_type = ''ORDR'' '
    || 'AND op.payment_method = ''CRCD'' '
    || 'AND op.payment_method_id = cc.credit_card_id '
    || 'AND cc.home_phone = :v_search_value1_in '
    IF (outcursor%ISOPEN) THEN
    CLOSE outcursor;
    END IF;
    OPEN outcursor FOR
    'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
    USING v_search_value1_in;
    ELSIF UPPER(v_search_type_in) = 'SHIPPINGPHONE'
    THEN
    v_sql_from := v_sql_from || ' ,shipping_info si '
    v_sql_where := v_sql_where || ' ' || v_group_where
    || 'AND po.order_id = si.order_id '
    || 'AND si.ship_phone = :v_search_value1_in '
    IF (outcursor%ISOPEN) THEN
    CLOSE outcursor;
    END IF;
    OPEN outcursor FOR
    'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
    USING v_search_value1_in;
    ELSIF UPPER(v_search_type_in) = 'CREDITCARDNUMBER'
    THEN
    v_sql_from := v_sql_from || ' ,order_payment op '
    || ' ,credit_card cc '
    v_sql_where := v_sql_where || ' ' || v_group_where
    || 'AND po.order_id = op.business_object_id '
    || 'AND op.business_object_type = ''ORDR'' '
    || 'AND op.payment_method = ''CRCD'' '
    || 'AND op.payment_method_id = cc.credit_card_id '
    || 'AND cc.credit_card_number = :v_search_value1_in '
    IF (outcursor%ISOPEN) THEN
    CLOSE outcursor;
    END IF;
    OPEN outcursor FOR
    'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
    USING v_search_value1_in;
    ELSIF UPPER(v_search_type_in) = 'PONUMBER'
    THEN
    v_sql_from := ' '
    || 'FROM ' || ' product_order_detail_actv_v pod ,' || SUBSTR(v_sql_from, 6);
    v_sql_where := v_sql_where || v_group_where
    || 'AND pod.po_no = :v_search_value1_in '
    || 'AND po.order_id = pod.order_id '
    IF (outcursor%ISOPEN) THEN
    CLOSE outcursor;
    END IF;
    OPEN outcursor FOR
    'SELECT /*+ ORDERED */ DISTINCT ' || v_sql_select || v_sql_from || v_sql_where
    USING v_search_value1_in;
    ELSIF UPPER(v_search_type_in) = 'USERASSIGNED'
    THEN
    IF v_search_value1_in = '0' -- 'Unassigned'
    THEN
    v_sql_where := v_sql_where || ' ' || v_group_where || ' ' || v_status_where
    || 'AND pog.unassigned_flag = ''Y'' '
    IF (outcursor%ISOPEN) THEN
    CLOSE outcursor;
    END IF;
    OPEN outcursor FOR
    'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
    USING v_open_status, v_requested_status, v_cancelled_status, v_closed_status;
    ELSE
    v_sql_where := v_sql_where || ' ' || v_group_where || ' ' || v_status_where
    || 'AND pog.dn_employee_id = :v_search_value1_in '
    IF (outcursor%ISOPEN) THEN
    CLOSE outcursor;
    END IF;
    OPEN outcursor FOR
    'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
    USING v_open_status, v_requested_status, v_cancelled_status, v_closed_status, v_search_value1_in;
    END IF;
    ELSIF UPPER(v_search_type_in) = 'ORDERDATE'
    THEN
    v_sql_where := v_sql_where || ' ' || v_group_where || ' ' || v_status_where
    || 'AND po.order_date BETWEEN TO_DATE( :v_search_value1_in, ''MON DD YYYY HH24:MI:SS'') AND TO_DATE( :v_search_value2_in ,''MON DD YYYY HH24:MI:SS'') '
    IF (outcursor%ISOPEN) THEN
    CLOSE outcursor;
    END IF;
    OPEN outcursor FOR
    'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
    USING v_open_status, v_requested_status, v_cancelled_status, v_closed_status, v_search_value1_in, v_search_value2_in;
    ELSIF UPPER(v_search_type_in) = 'PRINTJOB'
    THEN
    IF (outcursor%ISOPEN) THEN
    CLOSE outcursor;
    END IF;
    OPEN outcursor FOR
    'SELECT ' || v_sql_select
    || ' '
    || 'FROM product_order po '
    || ' ,bcf_print_jobs pj '
    || 'WHERE 1 = 1 '
    || 'AND pj.order_id = po.order_id '
    || 'AND pj.username = :v_search_value1_in '
    || 'AND pj.create_date = TO_DATE( :v_search_value2_in,''MON DD YYYY HH24:MI:SS'') '
    || v_sql_order_by
    USING v_search_value1_in, v_search_value2_in;
    ELSIF UPPER(v_search_type_in) = 'FULFILLMENTTYPE'
    THEN
    v_sql_from := v_sql_from || ' ,product_order_detail_actv_v pod ';
    v_sql_where := v_sql_where || ' ' || v_group_where || ' ' || v_status_where
    || 'AND pod.order_id = po.order_id '
    -- Because we have to drill down the the POD level
    -- we now need to ensure the pod records match on group
    || 'AND pod.group_id = ' || v_group_for_search
    -- We curr. have 3 fufillment types FC165, FC212, FCBABY
    -- so substr on 1st char works for Garry
    || 'AND UPPER(SUBSTR(pod.fulfillment_type,1,1)) = UPPER(:v_search_value1_in) '
    IF (outcursor%ISOPEN) THEN
    CLOSE outcursor;
    END IF;
    OPEN outcursor FOR
    'SELECT DISTINCT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by
    USING v_open_status, v_requested_status, v_cancelled_status, v_closed_status, v_search_value1_in;
    ELSIF UPPER(v_search_type_in) = 'INSUFFICIENTAUTHORIZATION'
    THEN
    v_sql_where := v_sql_where
    || 'AND bcf_get_authorization_amount(po.order_id) > 0'
    IF (outcursor%ISOPEN) THEN
    CLOSE outcursor;
    END IF;
    OPEN outcursor FOR
    'SELECT ' || v_sql_select || v_sql_from || v_sql_where || v_sql_order_by;
    END IF;
    END order_search;
    --

Maybe you are looking for

  • Vendor invoices are blocked automatically with payment blck A instead of R

    There is an issue with a specific vendor.This vendor's invoices are blocked automatically with payment block A instead of payment block R. I know that invoices are blocked when posted if there is a price variance exceeding 3% and/or 100 Euro with pay

  • Instalation of operating system

    I've been given a mac G4 400Hz, everything is in german, and I'd like to instal the operating system in english. I really don't know how to start, can I "format" everything like in a PC and instal afterwards the new operating system? Can somebody giv

  • Problem with audio config.?

    I've had this iPod...Video? Is that a 5th-generation? Whatever. Since...July or so last year. Maybe longer. And suddenly I'm getting this weird "iTunes cannot run because it has detected a problem with your audio configuration." Checked the audio. Wo

  • Mac pro power adapter separating

    I bought my mac pro in June and have been having an issue with the portable adapter separating where it plugs in. The _*whole plug falls out of the adapter (prongs and all)*_. It can be put back together and used, but I am afraid of short circuiting

  • IPad 2/airport express/traveling

    received airport express but still waiting for ipad 2 delivery. Need to set up airport so that I can hook it up to ethernet cable in hawaii. Do i have to wait for the ipad to set it up or can i use my macbook pro? Also, Since i already have a wireles