繁体   English   中英

我该如何写不同的程序

[英]How can I write this procedure differently

我想以不同的方式编写以下过程,以便我可以通过执行以下操作将其称为返回数据: SELECT * FROM table(package.get7DayCapacityDemandProv(1, sysdate))

Procesdure:

 PROCEDURE get7DayCapacityDemandProv(p_H_id                  IN     work_entity_data.H_id%TYPE
                                     ,p_date                         IN     DATE
                                     ,p_capacity_day_1                  OUT NUMBER
                                     ,p_demand_day_1                    OUT NUMBER
                                     ,p_capacity_day_2                  OUT NUMBER
                                     ,p_demand_day_2                    OUT NUMBER
                                     ,p_capacity_day_3                  OUT NUMBER
                                     ,p_demand_day_3                    OUT NUMBER
                                     ,p_capacity_day_4                  OUT NUMBER
                                     ,p_demand_day_4                    OUT NUMBER
                                     ,p_capacity_day_5                  OUT NUMBER
                                     ,p_demand_day_5                    OUT NUMBER
                                     ,p_capacity_day_6                  OUT NUMBER
                                     ,p_demand_day_6                    OUT NUMBER
                                     ,p_capacity_day_7                  OUT NUMBER
                                     ,p_demand_day_7                    OUT NUMBER
                                     )
  IS
  BEGIN

    getCapacityDemandOnDayProvider(p_H_id
                                  ,p_date
                                  ,p_capacity_day_1
                                  ,p_demand_day_1
                                  );

    getCapacityDemandOnDayProvider(p_H_id
                                  ,p_date + 1
                                  ,p_capacity_day_2
                                  ,p_demand_day_2
                                  );

    getCapacityDemandOnDayProvider(p_H_id
                                  ,p_date + 2
                                  ,p_capacity_day_3
                                  ,p_demand_day_3
                                  );

    getCapacityDemandOnDayProvider(p_H_id
                                  ,p_date + 3
                                  ,p_capacity_day_4
                                  ,p_demand_day_4
                                  );

    getCapacityDemandOnDayProvider(p_H_id
                                  ,p_date + 4
                                  ,p_capacity_day_5
                                  ,p_demand_day_5
                                  );

    getCapacityDemandOnDayProvider(p_H_id
                                  ,p_date + 5
                                  ,p_capacity_day_6
                                  ,p_demand_day_6
                                  );

    getCapacityDemandOnDayProvider(p_H_id
                                  ,p_date + 6
                                  ,p_capacity_day_7
                                  ,p_demand_day_7
                                  );

  END get7DayCapacityDemandProv;

您想要(1)将其转换为返回记录的函数,然后(2)将其转换为管道函数。

这是一个例子。 我已经省略了第一个参数,以便我可以轻松地运行它,但是您可以将其重新添加。

create or replace package test
as
  type theRecordType is record (
     day  date,
     capacity  number,
     demand  number
  );

  type theTableType is table of theRecordType;

  function getData(p_date DATE) return theTableType pipelined;
end test;
/

create or replace package body test
as
  function getData(p_date DATE) return theTableType pipelined
    as
      theRecord  theRecordType;
    begin
      for i in 0..6 loop
        theRecord.date := p_date + i;
        theRecord.capacity := i;
        theRecord.demand := i+1;
        --
        -- you would have a call to your procedure instead of the above two lines
        --      getCapacityDemandOnDayProvider(p_H_id
        --                          ,theRecord.date
        --                          ,theRecord.capacity
        --                          ,theRecord.demand
        --                          );
        --
        pipe row (theRecord);
      end loop;
      return;
    end getData;
end test;
/

现在,您可以从函数中进行选择,每天获取一行。

select * from table(test.getData(SYSDATE));

我将其打包,以便可以在包头中声明类型。 另外,您可以将其保留为独立函数,并使用CREATE TYPE在模式中声明CREATE TYPE

这是没有根据的,因此在语法上不会是100%正确的,但是在概念上是正确的。

create or replace package bingo IS
TYPE bingoCursor is REF CURSOR;

function get7Days(
    bingoId IN  bingoTable.bingoId%TYPE,
    bingoDate   IN  date)
    return bingoCursor;
end bingo;

create or replace package body bingo IS

function get7Days(
    bingoId IN  bingoTable.bingoId%TYPE,
    bingoDate   IN  date)
    return bingoCursor IS

    sevenDaysContent bingoCursor;
begin
    open sevenDaysContent for
        select day 1 stuff;

        union all

        select day 2 stuff;

        union all

        ... select and union all days 3 - day 7;

    return sevenDaysContent;
end get7Days;
end bingo;

听起来像您想要功能或视图。 过程的返回数据可以在SQL脚本中捕获和使用,但需要先将其转储到临时表或变量表中。

如果您的要求是仅需执行SELECT * FROM something那么您可能需要一个函数或一个视图。

您可以创建一个返回sys_refcursor的函数。 一个过于简单的示例:

create or replace function BLAH(somevar in varchar2) return sys_refcursor IS
v_result_cur sys_refcursor;
v_query varchar2(4000);
...
begin
...
v_query := 'select field1, field2 from blah';
...
open v_result_cur for v_query;
return v_result_cur;
...
exception
...
end;

但是我会说这不是典型的。 我可能会使用一个视图(或物化视图),或者将那些内部过程转换为函数并简单地选择它们,例如:

select
FN_getCapacityDemandOnDayProvider(someVars) as Day1Val,
FN_getCapacityDemandOnDayProvider(someVars2) as Day2Val
from dual;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM