1. 程式人生 > >Oracle工作紀要5

Oracle工作紀要5

CREATE OR REPLACE PACKAGE WS_CHECKUSER_TABLE IS

  -- Author  : ADMINISTRATOR
  -- Created : 2018/11/7 09:22:42
  --檢視1 檢視2 結果狀態 使用者名稱稱1  使用者名稱稱2
  --建立儲存過程 查詢使用者的所有表的差集 目前定位 syscd 和base
  --USER_CHECK_LEFT  VARCHAR2,  USER_CHECK_RIGHT VARCHAR2  暫時不需要
  TYPE PUBLICK_USER_TABLES_LEFT IS REF CURSOR;
  TYPE PUBLICK_USER_TABLES_RIGHT IS REF CURSOR;
  TYPE PUBLICK_USER_TABLES_SAME IS REF CURSOR;
  TYPE PUBLICK_ATTRIBUTE_TABLES IS REF CURSOR;
  ---查詢單個表的差異
  ---根據輸入的表,進行表的對比對比出來表的差異
  ---返回2個查詢的結果集
  ---表的名稱,表的庫字首和表查詢的類別。
  
  PROCEDURE TWOCOMPARISONS(P_CUR                OUT BASE_CONSTANT.T_CUR,
                           P_CUR2               OUT PUBLICK_ATTRIBUTE_TABLES,
                           RESULTMESSAGE      OUT VARCHAR2,
                           TABLENAMECHECK_LEFT VARCHAR2,
                           TABLENAMECHECK_RIGHT VARCHAR2, 
                           TABLENAMECHECK_LEFT_P VARCHAR2,
                           TABLENAMECHECK_RIGHT_P VARCHAR2,
                           P_QUERY_TYPE         NUMBER DEFAULT 0);  
  ---查詢表的名稱
  ---left 查詢 syscd 下有base 下沒有的表名稱
  ---right查詢 base  下有syscd下沒有的表名稱
  ---CENTER   syscd 下和base 下都有的表名稱    
  ---不傳參            
  PROCEDURE USER_TABLE_COMPARISONS(P_CUR_LEFT    OUT PUBLICK_USER_TABLES_LEFT,
                                   P_CUR_RIGHT   OUT PUBLICK_USER_TABLES_RIGHT,
                                   P_CUR_CENTER  OUT PUBLICK_USER_TABLES_SAME,
                                   RESULTMESSAGE OUT VARCHAR2);
  ---查詢準備進階版本 半完成狀態 
  PROCEDURE USER_TABLE_COLUMN_COMPARISONS(P_CUR_LEFT  OUT PUBLICK_USER_TABLES_LEFT,
                                          P_CUR_RIGHT OUT PUBLICK_USER_TABLES_RIGHT);
  --查詢syscd下和base下所有公共表的資料的差異sql語句組合表集
  --傳入引數為GENERATION_ID 和CD_SET  必須
  --返回結果集為表TABLEID(序號),TABLENAME(表名稱),TABLESQL(對應的sql語句,2個表沒有相同列名稱 則為空),
  ---TABLESTART(語句狀態 true:有共同列,有語句,false ;沒有公共列,沒有語句)
  ---準備進階的目標,查詢出來差異結果是否有數量,然後進行判斷是否需要進行顯示出來
  PROCEDURE USER_TABLE_COLUMN__SQL( 
                                   GENERATION_ID IN NUMBER    ,
                                   CD_SET IN NUMBER   ,     
                                   P_CUR_DB OUT PUBLICK_USER_TABLES_RIGHT);
                                   
  
                                  
END WS_CHECKUSER_TABLE;
CREATE OR REPLACE PACKAGE BODY WS_CHECKUSER_TABLE IS
-------------儲存過程查詢單個表的
 PROCEDURE TWOCOMPARISONS(P_CUR              OUT BASE_CONSTANT.T_CUR,
                           P_CUR2             OUT PUBLICK_ATTRIBUTE_TABLES,  
                           RESULTMESSAGE OUT  VARCHAR2,               
                           TABLENAMECHECK_LEFT VARCHAR2,
                           TABLENAMECHECK_RIGHT VARCHAR2, 
                           TABLENAMECHECK_LEFT_P VARCHAR2,
                           TABLENAMECHECK_RIGHT_P VARCHAR2,
                           P_QUERY_TYPE       NUMBER DEFAULT 0 
                           ) AS  
                           TWO_COUNT NUMBER;
                           ONE_COLUMN VARCHAR2(3999);
                          
                           SELECT_TYPE_ZERO   VARCHAR2(3999);      
                           SELECT_TYPE_ONE   VARCHAR2(3999);
                           SELECT_TYPE_TWO   VARCHAR2(3999);
                           
                         
  BEGIN 
        --******************************-- 
        
        --******************************--
                       
        ---查詢兩表的公共列                 
        SELECT COUNT(*) INTO TWO_COUNT FROM(
        SELECT * FROM( SELECT  COLUMN_NAME  FROM ALL_TAB_COLS WHERE TABLE_NAME =  TABLENAMECHECK_LEFT) tb1 
        INTERSECT
        SELECT * FROM( SELECT  COLUMN_NAME  FROM ALL_TAB_COLS WHERE TABLE_NAME =  TABLENAMECHECK_RIGHT) tb2 );
        ---查詢兩表的公共列
        IF TWO_COUNT <= 0 THEN
          --標識沒有相同的列直接不查詢 返回空的結果集
          P_CUR:= NULL;
        ELSE 
           --標識有相同的直 分條件判斷
             --查詢公共的列部分
             SELECT WMSYS.WM_CONCAT(COLUMN_NAME) INTO ONE_COLUMN FROM (
              SELECT * FROM( SELECT  COLUMN_NAME  FROM ALL_TAB_COLS WHERE TABLE_NAME = TABLENAMECHECK_LEFT) tb1 
              INTERSECT 
              SELECT * FROM( SELECT  COLUMN_NAME  FROM ALL_TAB_COLS WHERE TABLE_NAME = TABLENAMECHECK_RIGHT) tb2 
              ) ;
             --查詢公共的列部分
          SELECT_TYPE_ONE :=   'SELECT '|| ONE_COLUMN ||' FROM '||TABLENAMECHECK_LEFT_P||'.'||TABLENAMECHECK_LEFT||' MINUS SELECT  '|| ONE_COLUMN ||' FROM '||TABLENAMECHECK_RIGHT_P||'.'||TABLENAMECHECK_RIGHT;
          SELECT_TYPE_TWO :=   'SELECT '|| ONE_COLUMN ||' FROM '||TABLENAMECHECK_RIGHT_P||'.'||TABLENAMECHECK_RIGHT||' MINUS SELECT  '|| ONE_COLUMN ||' FROM '||TABLENAMECHECK_LEFT_P||'.'||TABLENAMECHECK_LEFT;
        
          IF P_QUERY_TYPE = 0 THEN 
           OPEN P_CUR FOR
             SELECT * FROM USER_TABLES;
        
          ELSIF P_QUERY_TYPE = 1 THEN
            -----1-----------
            OPEN P_CUR FOR
             SELECT_TYPE_ONE ;
              RESULTMESSAGE:= '查詢成功';
             -----1-----------                                                               
          ELSIF P_QUERY_TYPE = 2 THEN
            OPEN P_CUR FOR
              SELECT_TYPE_TWO ;
              RESULTMESSAGE:= '查詢成功';
          
          ELSE
               P_CUR :=NULL;
               RESULTMESSAGE:= '查詢失敗';
            
          END IF;
           --標識有相同的直 分條件判斷
        END IF;
            --******************************--
        
         
           
   
  END TWOCOMPARISONS;
-------------儲存過程查詢單個表的
  ---left 以syscd為主 查詢的差集 syscd 有的 base 沒有 right 以syscd為主 查詢的差集  base 有的 syscd  沒有 。center 為2個使用者 共有的表
  PROCEDURE USER_TABLE_COMPARISONS(P_CUR_LEFT    OUT PUBLICK_USER_TABLES_LEFT,
                                   P_CUR_RIGHT   OUT PUBLICK_USER_TABLES_RIGHT,
                                   P_CUR_CENTER  OUT PUBLICK_USER_TABLES_SAME,
                                   RESULTMESSAGE OUT VARCHAR2) AS
    ---USER_TABLE_COMPARISONS邏輯開始---
    P_CUR_LEFT_SQL  VARCHAR2(3999);
    P_CUR_RIGHT_SQL VARCHAR2(3999);
    P_CUR_SAME_SQL  VARCHAR2(3999);
  
  BEGIN
    -------------***-------------
  
    P_CUR_LEFT_SQL := 'SELECT * FROM (SELECT SUBSTR(t.TABLE_NAME,3) FROM  all_tables t WHERE  owner=''EMASS_SYSCD'' AND t.TABLE_NAME LIKE ''S%'' ORDER BY  t.TABLE_NAME ) SYSCD_TABLE
                        MINUS 
                        SELECT * FROM (SELECT SUBSTR(t.TABLE_NAME,3) FROM  all_tables t WHERE  owner=''EMASS_BASE'' AND t.TABLE_NAME LIKE ''B%''  ORDER BY  t.TABLE_NAME)BASE_TABLE';
  
    P_CUR_RIGHT_SQL := 'SELECT * FROM (SELECT SUBSTR(t.TABLE_NAME,3) FROM  all_tables t WHERE  owner=''EMASS_BASE'' AND t.TABLE_NAME LIKE ''B%''  ORDER BY  t.TABLE_NAME)BASE_TABLE 
                        MINUS
                        SELECT * FROM (SELECT SUBSTR(t.TABLE_NAME,3) FROM  all_tables t WHERE  owner=''EMASS_SYSCD'' AND t.TABLE_NAME LIKE ''S%'' ORDER BY  t.TABLE_NAME ) SYSCD_TABLE';
  
    P_CUR_SAME_SQL := 'SELECT * FROM (SELECT SUBSTR(t.TABLE_NAME,3) FROM  all_tables t WHERE  owner=''EMASS_SYSCD'' AND t.TABLE_NAME LIKE ''S%'' ORDER BY  t.TABLE_NAME ) SYSCD_TABLE
                        INTERSECT
                        SELECT * FROM (SELECT SUBSTR(t.TABLE_NAME,3) FROM  all_tables t WHERE  owner=''EMASS_BASE'' AND t.TABLE_NAME LIKE ''B%''  ORDER BY  t.TABLE_NAME)BASE_TABLE';
  
    OPEN P_CUR_LEFT FOR P_CUR_LEFT_SQL;
    RESULTMESSAGE := '查詢出LEFT資料';
    DBMS_OUTPUT.PUT_LINE( '查詢出LEFT資料');  
    OPEN P_CUR_RIGHT FOR P_CUR_RIGHT_SQL;
    RESULTMESSAGE := '查詢出LEFT資料,查詢出RIGHT資料';
    DBMS_OUTPUT.PUT_LINE( '查詢出LEFT資料,查詢出RIGHT資料');  
    OPEN P_CUR_CENTER FOR P_CUR_SAME_SQL;
    RESULTMESSAGE := '查詢出LEFT資料,查詢出RIGHT資料,查詢出共有資料';
    DBMS_OUTPUT.PUT_LINE( '查詢出LEFT資料,查詢出RIGHT資料,查詢出共有資料');  
  
  END USER_TABLE_COMPARISONS;

  -------------***-------------
  ---USER_TABLE_COMPARISONS邏輯結束---
  ---USER_TABLE_COLUMN_COMPARISONS---
  PROCEDURE USER_TABLE_COLUMN_COMPARISONS(
                                   P_CUR_LEFT    OUT PUBLICK_USER_TABLES_LEFT,
                                   P_CUR_RIGHT   OUT PUBLICK_USER_TABLES_RIGHT
                                   ) AS
     INDEXS NUMBER(38) DEFAULT 0 ;
     USER_TABLE_NAME_LEFT VARCHAR2(100);
     USER_TABLE_NAME_RIGHT VARCHAR2(100);  
     
     USER_TABLE_NAME_Q_LEFT VARCHAR2(100);
     USER_TABLE_NAME_Q_RIGHT VARCHAR2(100);  
     TWINS_COUNT NUMBER;
     TWINS_TABLEORCOLUMN_COUNT NUMBER;
     TWINS_START VARCHAR2(100);
     TWINS_COLUMN VARCHAR2(3999);
     TWINS_SELECT_WHERE  VARCHAR2(3999);
     P_CUR_LEFT_SQL_COUNT  NUMBER;
     P_CUR_RIGHT_SQL_COUNT NUMBER;
     P_CUR_LEFT_SQL  VARCHAR2(3999);
     P_CUR_RIGHT_SQL VARCHAR2(3999);
     ---宣告查詢sy下和base下的具有相同的表資料 查詢 all_tables表在最高許可權使用者下
     CURSOR  C_EMP IS 
                        SELECT * FROM (SELECT SUBSTR(t.TABLE_NAME,3) TABLENAME FROM  all_tables t WHERE  owner='EMASS_SYSCD' AND t.TABLE_NAME LIKE 'S%' ORDER BY  t.TABLE_NAME ) SYSCD_TABLE
                        INTERSECT
                        SELECT * FROM (SELECT SUBSTR(t.TABLE_NAME,3) TABLENAME FROM  all_tables t WHERE  owner='EMASS_BASE' AND t.TABLE_NAME LIKE 'B%'  ORDER BY  t.TABLE_NAME)BASE_TABLE;
                    
     C_ROW C_EMP%ROWTYPE;   
     ---宣告查詢sy下和base下的具有相同的表資料 查詢 all_tables表在最高許可權使用者下
     
  BEGIN
    
    ---迴圈相同的表開始
    FOR C_ROW IN C_EMP LOOP  
    INDEXS :=INDEXS+1;
    --DBMS_OUTPUT.PUT_LINE(INDEXS ||  C_ROW.TABLENAME || '--' );  
    --DBMS_OUTPUT.PUT_LINE(INDEXS);  
    ---迴圈體內的邏輯處理
    USER_TABLE_NAME_LEFT := 'S_'||C_ROW.TABLENAME;--組合標準查詢表名
    USER_TABLE_NAME_RIGHT := 'B_'||C_ROW.TABLENAME;--組合標準查詢表名
    USER_TABLE_NAME_Q_LEFT := 'EMASS_SYSCD.S_'||C_ROW.TABLENAME;--組合標準查詢表名
    USER_TABLE_NAME_Q_RIGHT := 'EMASS_BASE.B_'||C_ROW.TABLENAME;--組合標準查詢表名
    ---0.先根據表名查詢出來公共的列個數
    SELECT COUNT(*) INTO TWINS_COUNT FROM(
        SELECT * FROM( SELECT  COLUMN_NAME  FROM ALL_TAB_COLS WHERE TABLE_NAME =  USER_TABLE_NAME_LEFT) tb1 
        INTERSECT
        SELECT * FROM( SELECT  COLUMN_NAME  FROM ALL_TAB_COLS WHERE TABLE_NAME =  USER_TABLE_NAME_RIGHT) tb2 
        );
      IF TWINS_COUNT = 0 THEN   DBMS_OUTPUT.PUT_LINE('索引'|| INDEXS||'表名'||C_ROW.TABLENAME ||'沒有公共列');  TWINS_START :='FALSE';     
      ELSE TWINS_START :='TRUE'; 
      END IF;  
    
      IF TWINS_START = 'TRUE' THEN
        ---1.查詢出來公共的表的列
         SELECT WMSYS.WM_CONCAT(COLUMN_NAME) INTO TWINS_COLUMN FROM (
              SELECT * FROM( SELECT  COLUMN_NAME  FROM ALL_TAB_COLS WHERE TABLE_NAME = USER_TABLE_NAME_LEFT) tb1 
              INTERSECT 
              SELECT * FROM( SELECT  COLUMN_NAME  FROM ALL_TAB_COLS WHERE TABLE_NAME = USER_TABLE_NAME_RIGHT) tb2 
          ) ;
          
          
          ---1.2 查詢出來公共的表的列的查詢條件
           /*  SELECT WMSYS.WM_CONCAT('''||COLUMN_NAME||''') INTO TWINS_SELECT_WHERE FROM (
              SELECT * FROM( SELECT  COLUMN_NAME  FROM ALL_TAB_COLS WHERE TABLE_NAME = USER_TABLE_NAME_LEFT) tb1 
              INTERSECT 
              SELECT * FROM( SELECT  COLUMN_NAME  FROM ALL_TAB_COLS WHERE TABLE_NAME = USER_TABLE_NAME_RIGHT) tb2 
              ) ;*/
          
         --2.根據公共的列進行判斷 判斷列名和型別是否一致
        /*SELECT COUNT(*) INTO TWINS_TABLEORCOLUMN_COUNT FROM(
        SELECT * FROM( SELECT  COLUMN_NAME, DATA_TYPE  FROM ALL_TAB_COLS WHERE TABLE_NAME =  USER_TABLE_NAME_LEFT) tb1 
        MINUS
        SELECT * FROM( SELECT  COLUMN_NAME, DATA_TYPE  FROM ALL_TAB_COLS WHERE TABLE_NAME =  USER_TABLE_NAME_RIGHT) tb2 
        ) ;*/
        
        ---這裡如果直接大於0 的話,就跳出
         /*SELECT COUNT(*)   FROM(
        SELECT * FROM( SELECT  COLUMN_NAME, DATA_TYPE  FROM ALL_TAB_COLS WHERE TABLE_NAME =   'S_APPORTIONMENT_CONDITION'
           AND  COLUMN_NAME IN('CONDITION_INDEX','DESTINATION_PARTICIPANT_ID','PRODUCT_TYPE','SOURCE_PARTICIPANT_ID','UD_SUBTYPE','UD_TYPE')
         ) tb1 
        MINUS
        SELECT * FROM( SELECT  COLUMN_NAME, DATA_TYPE  FROM ALL_TAB_COLS WHERE TABLE_NAME =   'B_APPORTIONMENT_CONDITION'
         AND  COLUMN_NAME IN('CONDITION_INDEX','DESTINATION_PARTICIPANT_ID','PRODUCT_TYPE','SOURCE_PARTICIPANT_ID','UD_SUBTYPE','UD_TYPE')
        ) tb2 
        ) ;*/
        
        
        ---
          P_CUR_LEFT_SQL :=
           ' 
           SELECT  '||TWINS_COLUMN||' FROM ( 
           SELECT   '||TWINS_COLUMN||'
           FROM EMASS_SYSCD.'||USER_TABLE_NAME_LEFT||' t WHERE t.GENERATION_ID = 21 AND t.CD_SET = 25
           ORDER BY   '||TWINS_COLUMN||'
           ) t
           MINUS 
           SELECT  '||TWINS_COLUMN||' FROM (
           SELECT   '||TWINS_COLUMN||'
           FROM EMASS_BASE.'||USER_TABLE_NAME_RIGHT||' t
           ORDER BY   '||TWINS_COLUMN||') t;';
           
          P_CUR_RIGHT_SQL :=
             ' 
             SELECT  '||TWINS_COLUMN||' FROM (
           SELECT   '||TWINS_COLUMN||'
           FROM EMASS_BASE.'||USER_TABLE_NAME_RIGHT||' t
           ORDER BY   '||TWINS_COLUMN||') t
            MINUS  
           SELECT  '||TWINS_COLUMN||' FROM ( 
           SELECT   '||TWINS_COLUMN||'
           FROM EMASS_SYSCD.'||USER_TABLE_NAME_LEFT||' t WHERE t.GENERATION_ID = 21 AND t.CD_SET = 25
           ORDER BY   '||TWINS_COLUMN||'
           ) t; ';
          DBMS_OUTPUT.PUT_LINE('這個是第'||INDEXS); 
          IF INDEXS = 1 THEN
              DBMS_OUTPUT.PUT_LINE('這個是第'||P_CUR_LEFT_SQL); 
               DBMS_OUTPUT.PUT_LINE('這個是第'||P_CUR_RIGHT_SQL); 
          END IF ;
          --   DBMS_OUTPUT.PUT_LINE(P_CUR_LEFT_SQL); 
          
           --  DBMS_OUTPUT.PUT_LINE(P_CUR_RIGHT_SQL); 
         
          ---這裡如果直接大於0 的話,就跳出
      /*  IF TWINS_TABLEORCOLUMN_COUNT != TWINS_COUNT THEN
           DBMS_OUTPUT.PUT_LINE('索引'|| INDEXS||'表名'||C_ROW.TABLENAME ||'列名的資料型別不同'); 
        ELSIF TWINS_TABLEORCOLUMN_COUNT <=0 THEN 
            DBMS_OUTPUT.PUT_LINE('索引'|| INDEXS||'表名'||C_ROW.TABLENAME ||'資料型別不一致'); 
        ELSE   
        --3.在這裡是列名和列名資料型別都一樣。在這裡進行查詢差集,返出差集的結果
         DBMS_OUTPUT.PUT_LINE('--'); 
         \*   OPEN P_CUR_LEFT FOR P_CUR_LEFT_SQL;
            OPEN P_CUR_RIGHT FOR P_CUR_RIGHT_SQL;*\
         -- SELECT COUNT(*) INTO P_CUR_LEFT_SQL_COUNT FROM P_CUR_LEFT; 
         --3.
        END IF; */
         ---2.進行查詢相關的差集
      END IF;                          
   
    
    ---迴圈體內的邏輯處理
    END LOOP;  
    ---迴圈相同的表結束
    DBMS_OUTPUT.PUT_LINE(INDEXS);  
  END USER_TABLE_COLUMN_COMPARISONS;
  ---USER_TABLE_COLUMN_COMPARISONS---
  
  ---USER_TABLE_COLUMN_COMPARISONS---
  PROCEDURE USER_TABLE_COLUMN__SQL(
                                   GENERATION_ID IN NUMBER    ,
                                   CD_SET IN NUMBER    ,                   
                                   P_CUR_DB   OUT PUBLICK_USER_TABLES_RIGHT
                                   ) AS
     INDEXS NUMBER(38) DEFAULT 0 ;
     USER_TABLE_NAME_LEFT VARCHAR2(100);
     USER_TABLE_NAME_RIGHT VARCHAR2(100);  
     TWINS_LEFT_COUNT NUMBER;
     TWINS_RIGHT_COUNT NUMBER;
     TWINS_COUNT NUMBER;
    
     TWINS_START VARCHAR2(100);
     TWINS_COLUMN VARCHAR2(3999);
   
     P_CUR_LEFT_SQL  VARCHAR2(3999);
     P_CUR_RIGHT_SQL VARCHAR2(3999);
     ---臨時表的資料
     OUT_TABLE_DB_SQL  VARCHAR2(3999);
     OUT_INDERT_SQL VARCHAR2(3999);
     OUT_TABLE_DB_START VARCHAR2(100);
     ---宣告查詢sy下和base下的具有相同的表資料 查詢 all_tables表在最高許可權使用者下
     CURSOR  C_EMP IS 
                        SELECT * FROM (SELECT SUBSTR(t.TABLE_NAME,3) TABLENAME FROM  all_tables t WHERE  owner='EMASS_SYSCD' AND t.TABLE_NAME LIKE 'S%' ORDER BY  t.TABLE_NAME ) SYSCD_TABLE
                        INTERSECT
                        SELECT * FROM (SELECT SUBSTR(t.TABLE_NAME,3) TABLENAME FROM  all_tables t WHERE  owner='EMASS_BASE' AND t.TABLE_NAME LIKE 'B%'  ORDER BY  t.TABLE_NAME)BASE_TABLE;
                    
     C_ROW C_EMP%ROWTYPE;   
     ---宣告查詢sy下和base下的具有相同的表資料 查詢 all_tables表在最高許可權使用者下
     
  BEGIN
    ---建立儲存表 邏輯部分
     SELECT  COUNT(*) INTO OUT_TABLE_DB_START  FROM  user_tables WHERE  table_name = 'OUT_TABLE_DB';
     IF OUT_TABLE_DB_START <= 0 THEN 
 /*只存在第一次建立的時候用*/
  OUT_TABLE_DB_SQL :='CREATE GLOBAL TEMPORARY TABLE  OUT_TABLE_DB  (
       TABLEID NUMBER,
       TABLENAME VARCHAR2(500),
       TABLESQL VARCHAR2(3999),
       TABLESTART VARCHAR2(100)
    ) ON COMMIT PRESERVE ROWS ';
  EXECUTE IMMEDIATE OUT_TABLE_DB_SQL; 
  /*ELSE 
    OUT_TABLE_DB_SQL := 'DELETE FROM OUT_TABLE_DB';
    EXECUTE IMMEDIATE OUT_TABLE_DB_SQL; */
  END IF; 
    ---建立儲存表 邏輯部分
    
    ---迴圈相同的表開始
    FOR C_ROW IN C_EMP LOOP  
    INDEXS :=INDEXS+1;
    --DBMS_OUTPUT.PUT_LINE(INDEXS ||  C_ROW.TABLENAME || '--' );  
    --DBMS_OUTPUT.PUT_LINE(INDEXS);  
    ---迴圈體內的邏輯處理
    USER_TABLE_NAME_LEFT := 'S_'||C_ROW.TABLENAME;--組合標準查詢表名
    USER_TABLE_NAME_RIGHT := 'B_'||C_ROW.TABLENAME;--組合標準查詢表名
 
    ---0.先根據表名查詢出來公共的列個數
    SELECT COUNT(*) INTO TWINS_COUNT FROM(
        SELECT * FROM( SELECT  COLUMN_NAME  FROM ALL_TAB_COLS WHERE TABLE_NAME =  USER_TABLE_NAME_LEFT) tb1 
        INTERSECT
        SELECT * FROM( SELECT  COLUMN_NAME  FROM ALL_TAB_COLS WHERE TABLE_NAME =  USER_TABLE_NAME_RIGHT) tb2 
        );
      IF TWINS_COUNT = 0 THEN   
        DBMS_OUTPUT.PUT_LINE('索引'|| INDEXS||'表名'||C_ROW.TABLENAME ||'沒有公共列'); 
           OUT_INDERT_SQL :='INSERT INTO OUT_TABLE_DB(TABLEID,TABLENAME,TABLESQL,TABLESTART) VALUES('||INDEXS||','''||USER_TABLE_NAME_LEFT||''','''',''FALSE'')';
            EXECUTE IMMEDIATE OUT_INDERT_SQL;
             OUT_INDERT_SQL :='INSERT INTO OUT_TABLE_DB(TABLEID,TABLENAME,TABLESQL,TABLESTART) VALUES('||INDEXS||','''||USER_TABLE_NAME_RIGHT||''','''',''FALSE'')';
            EXECUTE IMMEDIATE OUT_INDERT_SQL;
         TWINS_START :='FALSE';     
      ELSE TWINS_START :='TRUE'; 
      END IF;  
    
      IF TWINS_START = 'TRUE' THEN
        ---1.查詢出來公共的表的列 例如:  t.GENERATION_ID = 21 AND t.CD_SET = 25
         SELECT WMSYS.WM_CONCAT(COLUMN_NAME) INTO TWINS_COLUMN FROM (
              SELECT * FROM( SELECT  COLUMN_NAME  FROM ALL_TAB_COLS WHERE TABLE_NAME = USER_TABLE_NAME_LEFT) tb1 
              INTERSECT 
              SELECT * FROM( SELECT  COLUMN_NAME  FROM ALL_TAB_COLS WHERE TABLE_NAME = USER_TABLE_NAME_RIGHT) tb2 
          ) ; 
          P_CUR_LEFT_SQL :=  'SELECT  '||TWINS_COLUMN||' FROM ( 
           SELECT   '||TWINS_COLUMN||'
           FROM EMASS_SYSCD.'||USER_TABLE_NAME_LEFT||' t WHERE t.GENERATION_ID = '||GENERATION_ID||' AND t.CD_SET =  '||CD_SET||'
           ORDER BY   '||TWINS_COLUMN||'
           ) t
           MINUS 
           SELECT  '||TWINS_COLUMN||' FROM (
           SELECT   '||TWINS_COLUMN||'
           FROM EMASS_BASE.'||USER_TABLE_NAME_RIGHT||' t
           ORDER BY   '||TWINS_COLUMN||') t;';
           
          P_CUR_RIGHT_SQL :=  'SELECT  '||TWINS_COLUMN||' FROM (
           SELECT   '||TWINS_COLUMN||'
           FROM EMASS_BASE.'||USER_TABLE_NAME_RIGHT||' t
           ORDER BY   '||TWINS_COLUMN||') t
           MINUS  
           SELECT  '||TWINS_COLUMN||' FROM ( 
           SELECT   '||TWINS_COLUMN||'
           FROM EMASS_SYSCD.'||USER_TABLE_NAME_LEFT||' t WHERE t.GENERATION_ID =  '||GENERATION_ID||' AND t.CD_SET =  '||CD_SET||'
           ORDER BY   '||TWINS_COLUMN||'
           ) t; ';
           
            OUT_INDERT_SQL :='INSERT INTO OUT_TABLE_DB(TABLEID,TABLENAME,TABLESQL,TABLESTART) VALUES('||INDEXS||','''||USER_TABLE_NAME_LEFT||''','''||P_CUR_LEFT_SQL||''',''TRUE'')';
            EXECUTE IMMEDIATE OUT_INDERT_SQL;
             OUT_INDERT_SQL :='INSERT INTO OUT_TABLE_DB(TABLEID,TABLENAME,TABLESQL,TABLESTART) VALUES('||INDEXS||','''||USER_TABLE_NAME_RIGHT||''','''||P_CUR_RIGHT_SQL||''',''TRUE'')';
            EXECUTE IMMEDIATE OUT_INDERT_SQL;
            --- 這個是
        
         
            
          DBMS_OUTPUT.PUT_LINE('這個是第'||INDEXS);  
      END IF;                          
   
    
    ---迴圈體內的邏輯處理
    END LOOP;  
    ---迴圈相同的表結束
    ---進行查處資料放到遊標中
    OUT_INDERT_SQL :='SELECT * FROM OUT_TABLE_DB';
    OPEN P_CUR_DB FOR 
    OUT_INDERT_SQL;
   
    ---進行查處資料放到遊標中
    DBMS_OUTPUT.PUT_LINE(INDEXS);  
  END USER_TABLE_COLUMN__SQL;
  ---USER_TABLE_COLUMN_COMPARISONS---
  
END WS_CHECKUSER_TABLE;