Parameter passing to custom SQL query using PL/SQL FUNCTION

Hi
In order to pass a parameter to the query in custom folder of a business area I created a function and mapped it to the Custome query using Discoverer Desktop. There is no error in mapping as the system does not throw any error. When I am inputting the Parameter for the input values everytime the query doesnot return any rows.
Can anybody help in this regard

Hi,
I need to take the request Id as input from the user and then fetch only the data pertaining to that requet Id. As a lot of complex joins are involved I need to pass request id as parameter to the custome folder.
The package i greated:
CREATE OR REPLACE PACKAGE SETPARAM
AS
param1 varchar2(25);
param2 varchar2(25);
FUNCTION SET_PARAM1(p1 IN varchar2) RETURN NUMBER ;
FUNCTION GET_PARAM1 RETURN varchar2;
END SETPARAM;
CREATE OR REPLACE PACKAGE BODY SETPARAM AS
FUNCTION SET_PARAM1(p1 IN varchar2) RETURN NUMBER IS
BEGIN
fnd_client_info.set_org_context('138');
param1 := p1;
dbms_output.put_line(param1);
RETURN 1;
END;
FUNCTION GET_PARAM1 RETURN varchar2 AS
BEGIN
RETURN param1;
END;
END SETPARAM;
I registered the set_param1 function as a pl/sql function in discoverer admin.
This function is called on the condition associated with the parameter in Discoverer Desktop when i run the report.
In the custom folder query i have this piece in the where clause
WHERE tnfo.request_id = NVL(APPS.SETPARAM.GET_PARAM1,7383588)
And everytime i get the data pertaining to request id =7383588,
Please suggest where i went wrong
thanks
Ashwini

Similar Messages

  • Optimizing an SQL Query using Oracle SQL Developer

    Hi ,
    Currently i am using Oracle SQL Developer as my Database IDE .
    Is it possible to use Orqcles SQLDeveloper for the purpose of Optimizing an SQL Query ??
    For example assume i am having a query as :
    Select from Tranac_Master where CUST_STATAUS='Y' and JCC_REPORT='N'*
    Could anybody please tell me how can i use Oracle SQL Developer to optimize this query or any other SQL queries ??
    Please share your ideas , thanks in advance .

    1. Your query looks very simplistic as it is, so I fail to see how you can better optimise it (unless 'Tranac_Master' is a view, in which case I'd need to see the view details).
    2. No tool can automagically optimise your SQL to any degree of practical use. Very minor adjustments may be possible automatically, but really it is a question of you knowing your data & database design accurately, and then you applying your expert knowledge to tune it.

  • 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

  • Crosstab query using pure SQL only

    Hi all,
    Found a lot of threads on crosstab, but none seems to address what I need. I need to perform crosstab query using pure SQL only & the number of columns are dynamic. From a query, I obtained the below table:
    Name Date Amount
    Alex 2005-06-10 1000
    Alex 2005-06-20 1000
    Alex 2005-07-10 1000
    Alex 2005-07-20 1000
    Alex 2005-08-10 1000
    Alex 2005-08-20 1000
    John 2005-06-10 2000
    John 2005-06-20 2000
    John 2005-07-10 2000
    John 2005-07-20 2000
    John 2005-08-10 2000
    John 2005-08-20 2000
    And I need to transform it into:
    Name 06-2005 07-2005 08-2005
    Alex 2000 2000 2000
    John 4000 4000 4000
    Reason for the columns being dynamic is because they'll be a limit on the date ranges to select the data from. I'd have a lower & upper bound date say June-2005 to August-2005, which explains how I got the data from the above table.
    Please advise.
    Thanks!

    Hi,
    I couldn't resist the intellectual challenge of a pure SQL solution for a pivot table with a dynamic number of columns. As Laurent pointed out, a SQL query can only have a fixed number of columns. You can fake a dynamic number of columns, though, by selecting a single column containing data at fixed positions.
    <br>
    <br>
    If it were me, I'd use a PL/SQL solution, but if you must have a pure SQL solution, here is an admittedly gruesome one. It shows the sum of all EMP salaries per department over a date range defined by start and end date parameters (which I've hardcoded for simplicity). Perhaps some of the techniques demonstrated may help you in your situation.
    <br>
    <br>
    set echo off
    set heading on
    set linesize 100
    <br>
    select version from v$instance ;
    <br>
    set heading off
    <br>
    column sort_order noprint
    column sal_sums format a80
    <br>
    select -- header row
      1        as sort_order,
      'DEPTNO' as DEPTNO ,
      sys_connect_by_path
        ( rpad
            ( to_char(month_column),
              10
          ' | '
        ) as sal_sums
    from
        select
          add_months( first_month, level - 1 ) as month_column
        from
          ( select
              date '1981-01-01' as first_month,
              date '1981-03-01' as last_month,
              months_between( date '1981-03-01', date '1981-01-01' ) + 1 total_months
            from dual
        connect by level < total_months + 1
      ) months
    where
      connect_by_isleaf = 1
    connect by
      month_column = add_months( prior month_column, 1 )
    start with
      month_column = date '1981-01-01'
    union all
    select -- data rows
      2 as sort_order,
      deptno,
      sys_connect_by_path( sum_sal, ' | ' ) sal_sums
    from
      select
        dept_months.deptno,
        dept_months.month_column,
        rpad( to_char( nvl( sum( emp.sal ), 0 ) ), 10 ) sum_sal
      from
          select
            dept.deptno,
            reporting_months.month_column
          from
            dept,
            ( select
                add_months( first_month, level - 1 ) as month_column
              from
                ( select
                    date '1981-01-01' as first_month,
                    date '1981-03-01' as last_month,
                    months_between( date '1981-03-01', date '1981-01-01' ) + 1 total_months
                  from
                    dual
              connect by level < total_months + 1
            ) reporting_months
        ) dept_months,
        emp
      where
        dept_months.deptno = emp.deptno (+) and
        dept_months.month_column = trunc( emp.hiredate (+), 'MONTH' )
      group by
        dept_months.deptno,
        dept_months.month_column
    ) dept_months_sal
    where
      month_column = date '1981-03-01'
    connect by
      deptno = prior deptno and
      month_column = add_months( prior month_column, 1 )
    start with
      month_column = date '1981-01-01'
    order by
      1, 2
    <br>
    VERSION
    10.1.0.3.0
    <br>
    DEPTNO      | 81-01-01   | 81-02-01   | 81-03-01
    10          | 0          | 0          | 0
    20          | 0          | 0          | 0
    30          | 0          | 2850       | 0
    40          | 0          | 0          | 0
    <br>
    Now, if we substitute '1981-03-01' with '1981-06-01', we see 7 columns instead of 4
    <br>
    DEPTNO      | 81-01-01   | 81-02-01   | 81-03-01   | 81-04-01   | 81-05-01   | 81-06-01
    10          | 0          | 0          | 0          | 0          | 0          | 2450
    20          | 0          | 0          | 0          | 2975       | 0          | 0
    30          | 0          | 2850       | 0          | 0          | 2850       | 0
    40          | 0          | 0          | 0          | 0          | 0          | 0
    <br>To understand the solution, start by running the innermost subquery by itself and then work your way outward.

  • 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

  • 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

  • Re: How to converting from PL/SQL query to T-SQL query

    How to converting from PL/SQL query to T-SQL query... Its Urgent

    Download the
    SQL Server Migration Assistant for Oracle.  It will convert whole Oracle databases, or single queries or PL/SQL stored procedures.
    With caution that If your database is using Collation which is case sensitive SSMA will not work.SSMA doesnt guarantees 100% for conversion of Queries/stored proc /database if it fails to do so for some queries you will have to do it manually.
    But you can try
    Please mark this reply as the answer or vote as helpful, as appropriate, to make it useful for other readers

  • Need sql query or pl/sql query urgent

    Hi Experts,
    The requirement is that
    SELECT 'N' flag, sysdate init_date,
    '' vendor_name,
    DECODE (pa.address_type,
    'P', 'Present Address',
    'R', 'Permanent Address',
    pa.address_type
    ) address_type,
    pa.address_line1
    || ','
    || pa.address_line2
    || ','
    || pa.town_or_city
    || ','
    || meaning
    || ','
    || pa.POSTAL_CODE "Address",
    TRUNC (TO_CHAR (pa.date_from, 'YYYY')) YEAR,
    TO_CHAR (pa.date_from, 'MON') MONTH,''station
    FROM per_addresses pa, fnd_lookup_values, per_all_people_f pf
    WHERE (pf.employee_number = :1 or pf.APPLICANT_NUMBER = :2)
    and pf.EFFECTIVE_END_DATE='31-DEC-4712'
    AND pa.person_id = pf.person_id
    AND pa.business_group_id = 42
    --AND pa.date_to IS NULL
    AND lookup_type = 'PER_US_COUNTRY_CODE'
    AND lookup_code = pa.country
    AND enabled_flag = 'Y'
    AND (end_date_active IS NULL OR end_date_active > SYSDATE)
    if i run the above query the output is coming like that
    Present Address |     Flat No. 1201, OC - 15, Orange County, Plot No. GH - 4,Ahinsa Khand 1st, Indrapuram,Ghaziabad,India,201010|     2,010|     JUL
    Permanent Address |     Q.No. 354, Sector - 3 / B,,Bokaro Steel City,India,827003     |2,010     |JUL
    Present Address |     4, Sitara,Chandra Reddy layout, S.T. Bed,Bangalore,India,0|     2,006|     JAN
    Present Address |     101,,Ushma Urja Apartments,Noida,India,201301 |     2,006 |     JUL
    Permanent Address |     F-19, Maccon Colony, Opp. DAV Shyamali School,,Ranchi,India,834002 |     2,009 |     FEB
    Present Address |     Flat no. 604, B - 1, Krishna Apra Gardens,Vaibhav Khand, Plot - 7,Ghaziabad,India,201010 |     2,009     FEB
    But the requirement is the output should come like that
    Permanent Address     |Q.No. 354, Sector - 3 / B,,Bokaro Steel City,India,827003|     2,010     |JUL                    
    Permanent Address 1|     F-19, Maccon Colony, Opp. DAV Shyamali School,,Ranchi,India,834002|     2,009 |     FEB
    Present Address |     Flat No. 1201, OC - 15, Orange County, Plot No. GH - 4,Ahinsa Khand 1st, Indrapuram,Ghaziabad,India,201010|     2,010 |     JUL                    
    Present Address 1 |     Flat no. 604, B - 1, Krishna Apra Gardens,Vaibhav Khand, Plot - 7,Ghaziabad,India,201010|     2,009 |     FEB                    
    Present Address 2 |     101,,Ushma Urja Apartments,Noida,India,201301|     2,006|     JUL     
    Present Address 3 |     4, Sitara,Chandra Reddy layout, S.T. Bed,Bangalore,India,0 |     2,006|     JAN     
    Please provide logice how i need to write a sql query or procedure or function or package
    Thanks & Regards
    Venu

    You can use analytics here :
    SELECT
        flag,
        init_date,
        vendor_name,
        address_type   ||' ' ||rn AS address_type,
        Address,
        YEAR,
        MONTH,
        station
    FROM
            SELECT
                'N' flag,
                sysdate init_date,
                '' vendor_name,
                DECODE (pa.address_type, 'P', 'Present Address', 'R','Permanent Address', pa.address_type ) address_type,
                row_number() over(partition BY pa.address_type order by 1) AS rn,
                pa.address_line1
                || ','
                || pa.address_line2
                || ','
                || pa.town_or_city
                || ','
                || meaning
                || ','
                || pa.POSTAL_CODE "Address",
                TRUNC (TO_CHAR (pa.date_from, 'YYYY')) YEAR,
                TO_CHAR (pa.date_from, 'MON') MONTH,
                '' station
            FROM
                per_addresses pa,
                fnd_lookup_values,
                per_all_people_f pf
            WHERE
                    pf.employee_number  = :1
                 OR pf.APPLICANT_NUMBER = :2
            AND pf.EFFECTIVE_END_DATE='31-DEC-4712'
            AND pa.person_id         = pf.person_id
            AND pa.business_group_id = 42
                --AND pa.date_to IS NULL
            AND lookup_type  = 'PER_US_COUNTRY_CODE'
            AND lookup_code  = pa.country
            AND enabled_flag = 'Y'
            AND
                    end_date_active IS NULL
                 OR end_date_active  > SYSDATE
        );

  • NVARCHAR (MAX) TO PRINT DYNAMIC SQL QUERY TO A SQL FILE

    Hi 
    I have a requirement where i need to write an SP which would construct a huge query using dynamic SQL and save the Dynamic query to a file. 
    The Dynamic SQL Variable I am using as @NVARCHAR(MAX) but since the query being built is large (>4000 characters), I am not able to select it into a table. PRINT @SQL prints the whole query but SELECT @SQL prints only upto 4000 characterrs. 
    And I need to save this huge dynamix sql to a file using the SP.
    Any thoughts as to how i can achieve this?
    Rajiv

    This is a know problem with dynamic SQL nvarchar(max) concatenation.
    See below for correct assembly of large SQL strings:
    CREATE table #temp(qry nvarchar(max));
    declare @cmd nvarchar(max);
    SELECT @cmd=CONCAT(CONVERT(nvarchar(max),N'SELECT '),
    CONVERT(nvarchar(max),REPLICATE (N'A',4000)),
    CONVERT(nvarchar(max),REPLICATE (N'A',4000)),
    CONVERT(nvarchar(max),REPLICATE (N'A',4000)),
    CONVERT(nvarchar(max),REPLICATE (N'A',4000)),
    CONVERT(nvarchar(max),N'FROM SYS.TABLES'));
    insert into #temp SELECT @cmd;
    select * from #temp;
    select len(qry), datalength(qry) from #temp;
    -- 16022 32044
    drop table #temp;
    Dynamic SQL:  http://www.sqlusa.com/bestpractices/dynamicsql/
    Kalman Toth Database & OLAP Architect
    SQL Server 2014 Design & Programming
    New Book / Kindle: Exam 70-461 Bootcamp: Querying Microsoft SQL Server 2012

  • SQL query using LIKE

    I want to do an SQL query which uses a parameter. This is the query: Find the books which contains the string "strTitle". I do this (it does not work):
    String strSQL="SELECT * FROM Books WHERE Title LIKE %'"+strTitle+"'% ";
    I know it is not correct, but anyone any idea of how i can do it?
    Thanks

    Hi,
    You can also use a PreparedStatement, for not-worrying about quotes:
    PreparedStatetment ps=con.prepareStatement(theQuery);
    ps.setString(1, likeString);
    ResultSet r=theQuery.execute();
    // etc...
    where :
    String theQuery=new String("SELECT * FROM PERSON WHERE NAME LIKE ?;");
    likeString='\%'+theStringSearching+'\%';
    also, likeString could be a custom method for making more complicated searches by one Like Statement,
    that needs reeding about RegExpressions.
    cheers, Thanasis

  • SQL Query using a Variable in Data Flow Task

    I have a Data Flow task that I created. THe source query is in this file "LPSreason.sql" and is stored in a shared drive such as
    \\servername\scripts\LPSreason.sql
    How can I use this .sql file as a SOURCE in my Data Flow task? I guess I can use SQL Command as Access Mode. But not sure how to do that?

    Hi Desigal59,
    You can use a Flat File Source adapter to get the query statement from the .sql file. When creating the Flat File Connection Manager, set the Row delimiter to a character that won’t be in the SQL statement such as “Vertical Bar {|}”. In this way, the Flat
    File Source outputs only one row with one column. If necessary, you can set the data type of the column from DT_STR to DT_TEXT so that the Flat File Source can handle SQL statement which has more than 8000 characters.
    After that, connect the Flat File Source to a Recordset Destination, so that we store the column to a SSIS object variable (supposing the variable name is varQuery).
    In the Control Flow, we can use one of the following two methods to pass the value of the Object type variable varQuery to a String type variable QueryStr which can be used in an OLE DB Source directly.
    Method 1: via Script Task
    Add a Script Task under the Data Flow Task and connect them.
    Add User::varQuery as ReadOnlyVariables, User::QueryStr as ReadWriteVariables
    Edit the script as follows:
    public void Main()
    // TODO: Add your code here
    System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter();
    DataTable dt = new DataTable();
    da.Fill(dt, Dts.Variables["User::varQuery"].Value);
    Dts.Variables["QueryStr2"].Value = dt.Rows[0].ItemArray[0];
    Dts.TaskResult = (int)ScriptResults.Success;
    4. Add another Data Folw Task under the Script Task, and join them. In the Data Flow Task, add an OLE DB Source, set its Data access mode to “SQL command from variable”, and select the variable User::QueryStr.
    Method 2: via Foreach Loop Container
    Add a Foreach Loop Container under the Data Flow Task, and join them.
    Set the enumerator of the Foreach Loop Container to Foreach ADO Enumerator, and select the ADO object source variable as User::varQuery.
    In the Variable Mappings tab, map the collection value of the Script Task to User::QueryStr, and Index to 0.
    Inside the Foreach Loop Container, add a Data Flow Task like step 4 in method 1.
    Regards,
    Mike Yin
    TechNet Community Support

  • 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;
    --

  • Query used by SQL Server Management Pack for monitoring database backups

    I use SCOM 2012 R2 and the SQL Server Management Pack to monitor SQL Server database backups. I believe I am getting false positives. SCOM reports database are not backuped, while in fact they are. So I need to troubleshoot this. I suspect SCOM is querying
    the backup history in the msdb database. I want to know which query SCOM uses.
    I have tried looking in the monitor's definition but I suspect the query is embedded in the management pack files which are binary. I have also tried running a trace using the SQL Server Profiler on my test environment and overriding the interval to 60 seconds,
    but I don't see a relevant query being executed. I also don't see the alert reappear so I suspect SCOM does not honor the interval in a way I would expect.
    What query, or other method, does SCOM use to check database backups?
    Thanks in advance.

    Thank you both Ivan and Michael,
    I only saw your messages by email and didn't see your screen shot before I extracted the query myself. In my own queries I calculate the backup age in hours instead of days because of daily full backups. Perhaps It will be a good idea to create my own monitors
    from scratch like I used to do with Nagios.
    I will study the vbscripts and might create my own version which allows the query to be entered as an parameter and move all code to a .Net class which can be called from vbscript as an COM object. This way I hope to reduce the vbscript code to an minimum
    while keeping the flexibility of the SCOM Operations Console and the robustness of .Net. I suspect I want to make more non standard monitors in the future.
    Regards,
    Arjen

  • Need SQL query using View - Please help

    Hi,
    I have similar requirement like below.
    I have two tables DEPT and EMP and some departments may not have employees. I have created below view, which displays all DEPT records, even though there are no emplyees.
    CREATE OR REPLACE VIEW dept_emp_vw AS
    SELECT deptno, empid, 0 AS selected
    FROM dept d, emp e
    WHERE d.deptno = e.deptnno (+);
    Ex.
    DEPTNO         EMPID        SELECTED
    10 101 0
    10 102 0
    20 103 0
    30 103 0
    40 104 0
    50 <null> 0
    Application will pass "empid" to the view (for ex. empid = 103) and I want result like below.
    Ex.
    DEPTNO         EMPID        SELECTED
    10 101 0
    10 102 0
    20 103 1
    30 103 1
    40 104 0
    50 <null> 0
    Can you please let me know the query using "dept_emp_vw" view. We have Oracle 11g Release 2.
    Thanks a lot for the help.

    Not possible using normal SQL - as SQL is not a procedure language and does not support variable declaration and use (e.g. passing empid as a variable and using it both as a predicate and as a condition in the SQL projection).
    That said - SQL can be "+parameterised+". An approach that is ugly and contrary to the basic design and use of SQL. But it can support the (very weird) view definition of yours.
    E.g.
    SQL> create or replace procedure SetVariable( name varchar2, value varchar2 ) is
      2  begin
      3          DBMS_SESSION.set_context( 'MyVariables', name, value );
      4  end;
      5  /
    Procedure created.
    SQL>
    SQL>
    SQL> create or replace context MyVariables using SetVariable;
    Context created.
    SQL>
    SQL> create or replace view my_funky_weird_view as
      2  select
      3          e.empno,
      4          e.ename,
      5          e.job,
      6          case e.empno
      7                  when to_number(sys_context( 'MyVariables', 'empid' )) then
      8                          0
      9                  else
    10                          1
    11          end     as "SELECTED"
    12  from       emp e
    13  /
    View created.
    SQL>
    SQL> exec SetVariable( 'empid', 7499 )
    PL/SQL procedure successfully completed.
    SQL>
    SQL> select * from session_context where namespace = 'MYVARIABLES';
    NAMESPACE            ATTRIBUTE            VALUE
    MYVARIABLES          EMPID                7499
    SQL>
    SQL> select * from my_funky_weird_view order by selected;
         EMPNO ENAME      JOB               SELECTED
          7499 ALLEN      SALESMAN                 0
          7521 WARD       SALESMAN                 1
          7566 JONES      MANAGER                  1
          7654 MARTIN     SALESMAN                 1
          7698 BLAKE      MANAGER                  1
          7934 MILLER     CLERK                    1
          7788 SCOTT      ANALYST                  1
          7839 KING       PRESIDENT                1
          7844 TURNER     SALESMAN                 1
          7876 ADAMS      CLERK                    1
          7900 JAMES      CLERK                    1
          7902 FORD       ANALYST                  1
          7369 SMITH      CLERK                    1
          7782 CLARK      MANAGER                  1
    14 rows selected.
    SQL>But I will N\OT recommend doing it this way. It is not natural SQL as PL/SQL is needed to "+inject+" name-value pairs into the context for the SQL view to use. It is ugly. It is not standard. It cannot scale. It is complex to use. Etc.
    Yes, there are instances when this approach is exactly what one needs - when for example dealing with a trusted context and using the contents for implementing a security layer. But in the above case - I would rather want to see the actual business requirement first, as I think you're barking up the wrong tree with the view solution you have imagined.

  • Where and how do I get the SQL Query used by the Microsoft Generic Report Library - Alerts?

    Background:
    I'm tasked with the following: I need to create a new Report and the SQL
    The two canned reports that I can pattern after are: Microsoft Generic Report Library
    - Alerts (there is also an alert detail report that can be chosen within the alert report that we may want to use instead)
    - Most Common Alerts
    I'm trying to do this:
    Add another parameter to search on customfield3.
    It should be able to report on all alerts that were assigned to the specific team for the time period along with the top 10 (most common alerts) assigned to the team for the time period.
    Choose as the objects (group) all servers, but Imay need to adjust the report to just look at all alerts without having to provide objects or a group
    The struggle I'm having is: I know SQL. I know how to create an RDL file.
    But Where are the RDL files for the canned reports so I can modify the canned RDL and modify its SQL and forms?
    What is the SQL/ where can I find the SQL used for the Generic Report Library -> Alerts

    Easy but you need to extract it from Microsoft Generic Report Pack. 
    So.. the procedure is as follows:
    1) You export and unseal Management Pack from your SCOM using
    Boris's OpsgMgr tools (MPViewer
    2.3.3)
    2) Ok you've got unsealed xml, now the tricky part, use
    MpElementsExtract tool, example of usage:
    MPElementsExtract.exe <MP.xml> /ex /destination:<destination>
    That you way you get several folders out of mp:
    DWScripts, DWSubScripts, LinkedReports, ReportResources, Reports
    Take a look into content of first 2, there will be pure sql scripts, and rdl's are in Reports folder :)
    Other way is to just use SQL profiler and catch SQL query while generating report. 
    --- Jeff (Netwrix)

Maybe you are looking for