SELECT * DISTINCT *与Oracle CURSOR和BULK COLLECT TO

时间:2014-01-23 15:31:01

标签: oracle plsql cursor bulk

请假设我使用以下代码:

  TYPE tb_MY_TABLE
  IS
     TABLE OF MY_TABLE%ROWTYPE
        INDEX BY PLS_INTEGER;

  tb_c3_MY_TABLE          tb_MY_TABLE;

  CURSOR c3_MY_TABLE (
     p3_IDENTIFIER_01   IN            VARCHAR2,
     p3_IDENTIFIER_02   IN            VARCHAR2
  )
  IS
     SELECT    IDENTIFIER_01,
               IDENTIFIER_02,
               STRING_01,
               STRING_02,
               STRING_03
       FROM   MY_TABLE
      WHERE   MY_TABLE.IDENTIFIER_01 = p3_IDENTIFIER_01
              AND MY_TABLE.IDENTIFIER_02 = p3_IDENTIFIER_02;

  OPEN c3_MY_TABLE (v_IDENTIFIER_01, v_IDENTIFIER_02);

  FETCH c3_MY_TABLE BULK COLLECT INTO   tb_c3_MY_TABLE;

  CLOSE c3_MY_TABLE;

  BEGIN
    FOR v_INDX_TER IN 1 .. tb_c3_MY_TABLE.COUNT

      LOOP
        ----- .....
      END LOOP;

  END;

如果c3_MY_TABLE在SELECT DISTINCT IDENTIFIER_01,IDENTIFIER_02来自MY_TABLE,我应该如何更改代码?

  CURSOR c3_MY_TABLE (
     p3_IDENTIFIER_01   IN            VARCHAR2,
     p3_IDENTIFIER_02   IN            VARCHAR2
  )
  IS
     SELECT    DISTINCT IDENTIFIER_01,
                        IDENTIFIER_02
       FROM   MY_TABLE
      WHERE   MY_TABLE.IDENTIFIER_01 = p3_IDENTIFIER_01
              AND MY_TABLE.IDENTIFIER_02 = p3_IDENTIFIER_02;

提前感谢您的善意建议!

1 个答案:

答案 0 :(得分:1)

请像这样更改您的声明(在DECLARE区块中),您就完成了。 DISTINCT适用于整个行SELECT,忽略TABLE中实际列数。它仅在选择过程之后发生。

CURSOR c3_MY_TABLE (
     p3_IDENTIFIER_01   IN            VARCHAR2,
     p3_IDENTIFIER_02   IN            VARCHAR2
  )
  IS
     SELECT    DISTINCT IDENTIFIER_01,
                        IDENTIFIER_02
       FROM   MY_TABLE
      WHERE   MY_TABLE.IDENTIFIER_01 = p3_IDENTIFIER_01
              AND MY_TABLE.IDENTIFIER_02 = p3_IDENTIFIER_02;


TYPE TY_ROW IS RECORD
(
   IDENTIFIER_01 MY_TABLE.IDENTIFIER_01%TYPE,
   IDENTIFIER_02 MY_TABLE.IDENTIFIER_02%TYPE
);

TYPE TY_TABLE is TABLE OF TY_ROW;

tb_c3_MY_TABLE TY_TABLE;