5

我有这个功能:

      Procedure UpdateDefaultWeight  ( vYear Number, costWeight Number, qualityWeight Number, serviceWeight Number ) 
      AS

        type weight_table is table of Number(5,2) index by varchar2(50);
        weightArray weight_table;
        currentPosition varchar2(50);
      Begin

        weightArray('Cost Weighting')    := costWeight;
        weightArray('Quality Weighting') := qualityWeight;
        weightArray('Service Weighting') := serviceWeight;

        currentPosition := weightArray.first;

        Loop
          Exit When currentPosition is null;
          Insert Into GVS.GVSSD16_DFLT_WEIGHT
            ( cal_year, metric_name, metric_val )
          Values
            ( vYear, currentPosition, weightArray(currentPosition) ); 

          currentPosition := weightArray.next(currentPosition);
        End Loop;
      END;

现在当我写它时,它只是做一个 INSERT。但是,我需要它来 UPSERT。我查看了有关 MERGE 的文档,但主要是让我对如何将语法应用于我的特定案例感到困惑。

我看过这里这里,我得到了它的要点,但语法让我失望。

有人愿意帮助 Oracle 新手吗?

4

1 回答 1

6

假设 cal_year 和 metric_name 定义了连接标准,这应该让你接近(未经测试):

MERGE INTO GVS.GVSSD16_DFLT_WEIGHT d
     USING (SELECT vYear AS YY,
                   currentPosition AS POS,
                   weightArray (currentPosition) AS WA
              FROM DUAL) v
        ON (d.cal_year = v.YY AND d.metric_name = v.pos)
WHEN MATCHED
THEN
   UPDATE SET metric_val = v.WA
WHEN NOT MATCHED
THEN
   INSERT     (cal_year, metric_name, metric_val)
       VALUES (v.YY, v.POS, v.WA);
于 2012-07-20T00:20:36.350 回答