0

我有允许有两列(CAGE,HCC)空值的表。我想在我的程序中显示重复的记录,我是这样写的。

 FOR REC IN (SELECT LOCATION, NIIN, INVL_DATE, CAGE, 
          HCC,SUM(CA_QTY) AS SUM_CA,SUM(COST_QTY) AS  SUM_COST,COUNT(*) FROM INVENTORY 
          GROUP BY LOCATION, NIIN, INVL_DATE,
          CAGE, HCC 
          HAVING COUNT(*)>1)
 LOOP  
  VAR_LOC_NAME    :=  REC.LOCATION;
  VAR_NIIN        :=  REC.NIIN;
  VAR_DATE        :=  REC.INVL_DATE;
  VAR_CAGE        :=  REC.CAGE; 
  VAR_HCC         :=  REC.HCC;
  VAR_CA_QTY      :=  REC.SUM_CA;
  VAR_COST_QTY    :=  REC.SUM_COST;

 FOR REC1 IN (SELECT SNO FROM INVENTORY WHERE LOCATION=VAR_LOC_NAME AND  
                NIIN=VAR_NIIN AND TUNC(INVL_DATE)=TRUNC(TO_DATE(VAR_DATE,'DD-MM-YY'))  AND
                CAGE=VAR_CAGE AND HCC=VAR_HCC)
    LOOP
        DBMS_OUTPUT.PUT_LINE('GET NUMBER '||REC1.SNO);
    END LOOP;
   end loop;

但对于 CAGE 和 HCC 的空值,它不起作用。仅供参考:我正在使用 oracle 11g

4

1 回答 1

0

您需要找到一些未在CAGEand列中使用的值,并使用 Oracle 的函数HCC将 NULL 值替换为该唯一值。NVL所以:

 FOR REC IN (SELECT LOCATION, NIIN, INVL_DATE, NVL(CAGE,-1) as CAGE, 
          NVL(HCC,-1) as HCC,SUM(CA_QTY) AS SUM_CA,SUM(COST_QTY) AS  SUM_COST,COUNT(*) FROM INVENTORY 
          GROUP BY LOCATION, NIIN, INVL_DATE,
          NVL(CAGE,-1), NVL(HCC,-1) 
          HAVING COUNT(*)>1)

当然-1这里只是一个示例。

至于第二个循环:

FOR REC1 IN (SELECT SNO FROM INVENTORY WHERE LOCATION=VAR_LOC_NAME AND  
            NIIN=VAR_NIIN AND TUNC(INVL_DATE)=TRUNC(TO_DATE(VAR_DATE,'DD-MM-YY'))  AND
            NVL(CAGE,-1)=VAR_CAGE AND NVL(HCC,-1)=VAR_HCC)

在打印输出上,您可以-1NULL.

于 2012-08-27T08:46:11.753 回答