簡體   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