更新集合

时间:2017-10-31 18:57:25

标签: sql oracle plsql

我想创建一个返回元素列表的PL / SQL函数。我已经创建了一个这样的新类型:

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;
/

有我的功能

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;
/

问题是,有时我想在结果选项卡中添加一个新行,有时我想更新结果表中的特定行,但更新不起作用。

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

错误是:ORA-00903:表名无效。

可以像这样更新吗?还是有另一种解决方案?

1 个答案:

答案 0 :(得分:0)

你不需要在更新语句中使用like table(tab_res),只需使用tab_res就足够如下:

                UPDATE tab_res
                   SET quantite=quantite+result.quantite
                 WHERE idIngredient = result.idIngredient;
相关问题