将脚本转换为PL / SQL过程

时间:2013-08-09 13:25:04

标签: oracle plsql oracle9i

我是Oracle PL SQL的新手。对于以下脚本,如何使其成为一个过程?我需要将下面的脚本转换为一个过程,以便我可以从一个应用程序(SSRS 2005)中调用它。该应用程序使用Oracle客户端驱动程序,无法运行下面的脚本,但可以运行程序。所以我想将脚本转换为一个过程。

如果没有Exec dbms_application_info.set_client_info('81');该应用程序可以运行脚本没问题。 P.S我可以在Toad中运行以下脚本,没问题。

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')

2 个答案:

答案 0 :(得分:4)

没有参数:

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;

使用一组参数:

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;

答案 1 :(得分:1)

您需要两个单独的脚本。

脚本1:定义存储过程

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;

脚本2:定义将由存储过程用于返回值的游标类型

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

编辑:正如在评论中正确报告使用Oracle 9i或更高版本 sys_refcursor可以使用而不是定义自定义类型,因此上面的脚本变为:< / p>

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;