Pregunta

I am new to Oracle PL SQL. For the following script, how to make it a procedure? I need to convert the script below to a procedure so that I can call it from an application (SSRS 2005). The application uses Oracle client driver and can't run the script below but can run procedures. So I am thinking to convert the script to a procedure.

If without the Exec dbms_application_info.set_client_info('81'); The script can be run by the application no problem. P.S I can run the below script in Toad, no problem.

Exec dbms_application_info.set_client_info('81');

SELECT 
    ooh.order_number Order_number
    , ool.ordered_item
    ,ooh.ordered_date
FROM     apps.oe_order_headers_all ooh
    , apps.oe_order_lines_all ool
WHERE 1 = 1
    AND ooh.header_id = ool.header_id
    AND ooh.order_number = '27889292'
    --AND ooh.ordered_date BETWEEN  TO_DATE('28-FEB-2013 00:00:00', 'DD-MON-YYYY HH24:MI:SS')  AND  TO_DATE('28-FEB-2013 23:59:59', 'DD-MON-YYYY HH24:MI:SS')-- 1.3
    and ooh.ordered_date >= TO_DATE('28-FEB-2013 00:00:00', 'DD-MON-YYYY HH24:MI:SS')
    and ooh.ordered_date < TO_DATE('01-MAR-2013 00:00:00', 'DD-MON-YYYY HH24:MI:SS')
¿Fue útil?

Solución

Without parameters:

create or replace function GetOrderInfo return sys_refcursor 
as
  vRet sys_refcursor;
begin

  dbms_application_info.set_client_info('81');

  open vRet for 
    select  
      ooh.order_number Order_number,
      ool.ordered_item,
      ooh.ordered_date
    from 
      apps.oe_order_headers_all ooh,
      apps.oe_order_lines_all   ool
    where
       ooh.header_id = ool.header_id
       AND ooh.order_number = '27889292'
       --AND ooh.ordered_date BETWEEN  
              TO_DATE('28-FEB-2013 00:00:00', 'DD-MON-YYYY HH24:MI:SS')  
              AND  
              TO_DATE('28-FEB-2013 23:59:59', 'DD-MON-YYYY HH24:MI:SS')-- 1.3
       and ooh.ordered_date >= TO_DATE('28-FEB-2013 00:00:00', 'DD-MON-YYYY HH24:MI:SS')
       and ooh.ordered_date < TO_DATE('01-MAR-2013 00:00:00', 'DD-MON-YYYY HH24:MI:SS')
  ;

  return vRet;

end;

With set of parameters:

create or replace function GetOrderInfo(
  pClientId    in varchar2,
  pFromDate    in date,
  pToDate      in date, 
  pOrderNumber in varchar2
) return sys_refcursor 
as
  vRet sys_refcursor;
begin

  dbms_application_info.set_client_info(pClientId);

  open vRet for 
    select  
      ooh.order_number Order_number,
      ool.ordered_item,
      ooh.ordered_date
    from 
      apps.oe_order_headers_all ooh,
      apps.oe_order_lines_all   ool
    where
       ooh.header_id = ool.header_id
       and ooh.order_number = pOrderNumber
       --and ooh.ordered_date BETWEEN pFromDate AND pToDate-- 1.3
       and ooh.ordered_date >= pFromDate
       and ooh.ordered_date < pToDate
  ;

  return vRet;

end;

Otros consejos

You'll need two separate scripts.

Script 1: define stored procedure

CREATE OR REPLACE PROCEDURE "PROCEDURE_NAME" (
    p_order_number  in number,
    p_start_date    in varchar2,
    p_end_date      in varchar2,
    p_cursor_out    out types.cursor_type
)
AS

BEGIN
    Exec dbms_application_info.set_client_info('81'); 

    open p_cursor_out for
    SELECT 
    ooh.order_number Order_number
     , ool.ordered_item
     ,ooh.ordered_date
      FROM     apps.oe_order_headers_all ooh
       , apps.oe_order_lines_all ool
      WHERE 1 = 1
       AND ooh.header_id = ool.header_id
      AND ooh.order_number = p_order_number
       and ooh.ordered_date >= TO_DATE(p_start_date, 'DD-MON-YYYY HH24:MI:SS')
       and ooh.ordered_date < TO_DATE(p_end_date, 'DD-MON-YYYY HH24:MI:SS')
END;

Script 2: define cursor type that will be used by the stored procedure to return values

create or replace
PACKAGE "TYPES" AS
  TYPE cursor_type IS REF CURSOR;
END Types; 

Edit: as correctly reported in comments using Oracle 9i or later sys_refcursor can be used instead of defining a custom type, so above script becomes:

CREATE OR REPLACE PROCEDURE "PROCEDURE_NAME" (
    p_order_number  in number,
    p_start_date    in varchar2,
    p_end_date      in varchar2,
    p_cursor_out    out sys_refcursor
)
AS

BEGIN
    dbms_application_info.set_client_info('81'); 

    open p_cursor_out for
    SELECT 
    ooh.order_number Order_number
     , ool.ordered_item
     ,ooh.ordered_date
      FROM     apps.oe_order_headers_all ooh
       , apps.oe_order_lines_all ool
      WHERE 1 = 1
       AND ooh.header_id = ool.header_id
      AND ooh.order_number = p_order_number
       and ooh.ordered_date >= TO_DATE(p_start_date, 'DD-MON-YYYY HH24:MI:SS')
       and ooh.ordered_date < TO_DATE(p_end_date, 'DD-MON-YYYY HH24:MI:SS')
END;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top