如何在PL / SQL循环中停止重复值/ Null值

时间:2017-05-25 15:51:16

标签: oracle loops stored-procedures plsql

我遇到的问题是在我的存储过程中输出空值和重复列。 目前,一个数组作为TYPE传递给我的过程。我需要从数组中输出所有部件号/ pdc / src。目前,如果TYPE(数组)中的记录与库存中的记录匹配,则没有问题。如果它们不存在,那么将仅为光标中的相应列输出空值。 我已尝试将当前记录插入变量并使用NVL运算符,但记录最终会重复。有没有人有任何建议?

PROCEDURE iws_update (i_Tbulk_type   IN  update_bulk_type,
                      o_Cresultset   OUT SYS_REFCURSOR)
AS
    partcount   VARCHAR2 (20) := NULL;
    sumcount    VARCHAR2 (4) := NULL;
    miscount    VARCHAR2 (1) := NULL;
BEGIN
    FOR i IN i_Tbulk_type.FIRST .. i_Tbulk_type.LAST
    LOOP
        partcount := i_Tbulk_type (i).part_no; --
        sumcount := i_Tbulk_type (i).pdc;
        miscount := i_Tbulk_type (i).part_no;

        UPDATE inventory i               -- update logic
        SET    i.diff_qty = i_Tbulk_type (i).mismatch_qty,
               i.aval_qty = ( (i.aval_qty + i.diff_qty) - i_Tbulk_type (i).mismatch_qty) 
        WHERE  i.dept_cd = i_Tbulk_type (i).pdc --First PK validation
        AND    i.src = i_Tbulk_type (i).src     --second PK validation
        AND    TRIM (i.part_no) = TRIM (i_Tbulk_type (i).part_no) -- third PK validation
        AND    i.diff_qty >= 0 -- second step in validation: diff qty must be greater than zero
        AND    ( (i.diff_qty + i.aval_qty) - i_Tbulk_type (i).mismatch_qty) >=
               0 ; -- Final condition that must be met: the sum of diff and aval quantity minus the qty passed in by the array must be greater than or equal to zero. If all conditions are met update proceeds.
        COMMIT;

        OPEN o_Cresultset FOR --cursor to send back records and flags to Back End
            SELECT t.pdc, -- array dept code
                   t.src, -- array source
                   t.part_no, -- array part no  
                   CASE WHEN i.part_no IS NULL THEN 'FAIL' ELSE 'PASS' END AS part_no_flag, -- if part no is null then it doesn't  exist in inventory table and will print 'FAIL' as a flag. else pass.
                   ( (i.diff_qty + i.aval_qty) - t.mismatch_qty) AS avail_qty,-- sends the quantity based on second validation method. line 440. If output is negative BE will mark as fail
                   i.diff_qty AS unmatch_qty -- prints qty. if negative BE marks as fail.
            FROM   TABLE (CAST (i_Tbulk_type AS update_bulk_type)) t
                   LEFT JOIN inventory i
                        ON   i.dept_cd = t.pdc
                        AND  i.src = t.src
                        AND  TRIM (i.part_no) = TRIM (t.part_no);
    END LOOP;
END iws_update;

0 个答案:

没有答案