0

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

如果没有 Exec dbms_application_info.set_client_info('81'); 该脚本可以由应用程序运行没有问题。PS 我可以在 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')
4

2 回答 2

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;
于 2013-08-09T16:15:44.753 回答
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可以使用而不是定义自定义类型,因此上面的脚本变为:

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;
于 2013-08-09T14:25:18.100 回答