2011-01-20 53 views
1

我想以不同的方式寫了下面的過程,所以我可以把它返回數據,就好像它是表做: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; 
+0

你的意思是你想要的SELECT返回一個排,14列,或做你希望它返回7行,每天一個,每個都有兩列? – 2011-01-20 13:49:18

+0

@dave編輯:理想情況下,我想3 columsn和7行。列=日,容量,需求。行= day1,day2,day3等,所以我可以把它放在圖中。如果沒有,那麼無論你能想到什麼,我只是想讓它脫離程序,並進入一個表,我可以從 – code511788465541441 2011-01-20 13:59:04

回答

0

聽起來像你想要的功能或視圖。過程的返回數據可以在SQL腳本中捕獲和使用,但它需要首先將其轉儲到臨時表或變量表中。

如果您的要求是能夠做一個SELECT * FROM something那麼你可能需要一個函數或視圖。

+0

中選擇是的,我需要一個包裝函數。你知道我可以如何執行這個特殊的程序嗎? – code511788465541441 2011-01-20 12:45:19

1

這是關閉的,所以它不會是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;
+0

我的程序的哪一部分在選擇日x位?即時通訊不是很熟悉plsql,你可以使它在語法上更合理一些,以適應我的程序。謝謝 – code511788465541441 2011-01-20 13:47:06

2

想要(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在模式中聲明類型。

0

您可以創建一個返回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;