2017-10-31 12 views
0

要素のリストを返すPL/SQL関数を作成したいと考えています。私の機能コレクションの更新

CREATE OR REPLACE FUNCTION genererListe(user IN INT, dateEstime IN date) 
RETURN isteIngredient_tab 
AS 
    tab_res isteIngredient_tab; 

    quantiteAacheter INT; 
    quantitePosseder INT :=0; 
    exist INT; 

-- Cursor on recipe 
    CURSOR cursor_recette IS 
    SELECT r.* FROM Recette r 
    JOIN planning p 
    ON p.idRecette=r.idrecette 
    JOIN Utilisateur u 
    ON p.idUtilisateur=u.idUtilisateur 
    WHERE u.idUtilisateur=1 
    AND p.jour>dateEstime; 

BEGIN 
    tab_res := isteIngredient_tab(); 

    FOR row_recette IN cursor_recette 
    LOOP 

    --Iterate on all ingredient of a recipe 
    FOR result IN (SELECT i.*,ir.quantite FROM Ingredient i 
        JOIN IngredientRecette ir 
        ON ir.idIngredient=i.idIngredient 
        JOIN Recette r 
        ON r.idRecette=ir.idRecette 
        WHERE i.Disponibilite = 'N' 
        AND r.idRecette=row_recette.idrecette) 
        LOOP 
        SELECT CASE 

        -- Check if an ingredient is already present in the tab_res 
        WHEN EXISTS(select * from table(tab_res) WHERE idIngredient = result.idIngredient) 
        THEN 1 
        ELSE 0 
        END INTO exist 
        FROM dual; 

        --If it is not present, add a new line in tab_res 
        IF exist = 0 THEN 
        tab_res.extend; 
        tab_res(tab_res.count) := listeIngredient_col(result.idIngredient, 
                    result.nom, 
                    5, 
                    dateEstime); 

        -- If it is present uprgrade quantity value 
        ELSE 

        UPDATE table(tab_res) 
        SET quantite=quantite+result.quantite 
        WHERE idIngredient = result.idIngredient; 
        END IF; 

      END LOOP; 
    END LOOP; 
    RETURN tab_res; 
END; 
/

問題はあるが、いつか私は結果]タブに新しい行を追加したいといつか私がしたい、

CREATE OR REPLACE TYPE listeIngredient_col AS OBJECT 
(
    idIngredient int, 
    nomIngredient varchar(25), 
    quantite int, 
    dateE date 
); 
/


CREATE OR REPLACE TYPE isteIngredient_tab IS TABLE OF listeIngredient_col; 
/

があります:私はすでにこのような新しいタイプを作成しました結果表の特定の行を更新しますが、更新は機能しません。

UPDATE table(tab_res) 
SET quantite=quantite+result.quantite 
WHERE idIngredient = result.idIngredient; 

エラーは次のとおりです。ORA-00903:無効な表名。

このように更新することはできますか?それとも別の解決策がありますか?更新文あちこち

+2

'NESTED'テーブルは' UPDATE'文で更新することはできません –

答えて

0

あなたは表のように使用する必要はありません(tab_res)、ちょうどtab_resを使用すると、以下のように十分です:

   UPDATE tab_res 
        SET quantite=quantite+result.quantite 
       WHERE idIngredient = result.idIngredient; 
関連する問題