程序执行方式上的oracle pl / sql问题

时间:2017-04-17 19:48:49

标签: oracle plsql oracle11g

我有一个方案我有一个表t1,它有两个表名sanman。现在两个表sanman每个表都有多个表文件名,如表san,有两个文件名(audi.txtmercedes.txt)和第二个表man有一个文件名(hundai.txt)。我编写了一个过程,它可以返回相应文件名中各个表中存在的行数。这是程序:

:sql查询

-- for creating t1 table--

CREATE TABLE HR.T1
(
  NAMES       VARCHAR2(20 BYTE),
  MAPPING_ID  VARCHAR2(10 BYTE)
);

SET DEFINE OFF;
Insert into HR.T1
   (NAMES, MAPPING_ID)
 Values
   ('san', '1');
Insert into HR.T1
   (NAMES, MAPPING_ID)
 Values
   ('man', '1');
COMMIT;

-----------sql query for 'san' table----

CREATE TABLE HR.SAN
(
  SRC_FILENAME  VARCHAR2(20 BYTE)
);

SET DEFINE OFF;
Insert into HR.SAN
   (SRC_FILENAME)
 Values
   ('audi.txt');
Insert into HR.SAN
   (SRC_FILENAME)
 Values
   ('mercedes.txt');
COMMIT;

------sql query for man table ----

CREATE TABLE HR.MAN
(
  SRC_FILENAME  VARCHAR2(20 BYTE)
);

SET DEFINE OFF;
Insert into HR.MAN
   (SRC_FILENAME)
 Values
   ('hundai.txt');
COMMIT;

-------package spec -----

CREATE OR REPLACE PACKAGE HR.file_entry

AS

PROCEDURE PKG_PROC_FILES(L_MAPPING_ID NUMBER); 
procedure insert_proc (l_object_name VARCHAR2);
END;

-----package body -----


    CREATE OR REPLACE PACKAGE BODY HR.file_entry
AS

   PROCEDURE PKG_PROC_FILES (L_MAPPING_ID NUMBER)

AS
      V_TABLE_NAME    VARCHAR2 (50);
  V_SCHEMA_NAME   VARCHAR2 (50);

  TYPE CURTYPE IS REF CURSOR;

  V_SCHEMA_NAME   VARCHAR2 (50);


     ----
      CURSOR TARGET_OBJ_CUR
      IS
         SELECT DISTINCT names
           FROM t1
          WHERE MAPPING_ID = L_MAPPING_ID;
   BEGIN

    FOR I IN TARGET_OBJ_CUR
      LOOP
         INSERT_PROC (I.names);
         DBMS_OUTPUT.PUT_LINE ('TARGET_TABLE_NAME= ' || I.names);
      END LOOP;
   END;



PROCEDURE INSERT_PROC (L_OBJECT_NAME VARCHAR2)

   AS

      V_TABLE_NAME       VARCHAR2 (50);
      V_SCHEMA_NAME      VARCHAR2 (50);
      V_QUERY            VARCHAR2 (50);

      TYPE CURTYPE IS REF CURSOR;

      V_SRC_FILE_NAMES   VARCHAR2 (200);
      CUR                CURTYPE;

BEGIN

      V_QUERY := 'select distinct src_filename from ' || L_OBJECT_NAME;
      OPEN CUR FOR V_QUERY;

      LOOP
         FETCH CUR INTO V_SRC_FILE_NAMES;

         DBMS_OUTPUT.PUT_LINE ('SOURCE FILE NAMES 1 = ' || V_SRC_FILE_NAMES);
         COMMIT;

         EXIT WHEN CUR%NOTFOUND;
      END LOOP;

      CLOSE CUR;

   END;

END;
/

执行程序后,我有多个DB名称:

O/P
SOURCE FILE NAMES = mercedes.txt
SOURCE FILE NAMES = audi.txt
SOURCE FILE NAMES = audi.txt
TARGET_TABLE_NAME= san
SOURCE FILE NAMES = hundai.txt
SOURCE FILE NAMES = hundai.txt
TARGET_TABLE_NAME= man

在下面的O / P中我需要从表中只获取不同的源文件名但我无法理解为什么我多次获得audi.txthundai.txt。任何人都可以帮我解决这个问题吗?我需要一个文件名一次打印在输出中,如mercedes.txt,它只在输出中打印过一次。

1 个答案:

答案 0 :(得分:1)

EXIT WHEN CUR%NOTFOUND;退出语句需要在fetch程序中的INSERT_PROC后检查,而不是end loop之前。为什么在commit之后使用dbms_output.put_line