Di seguito una query che genera la ddl per una minus tra due tabelle ed inserisce il contenuto in una tabella di appoggio.
SELECT
'INSERT INTO DM_ITI.APPO_COUNT_DIFF '||CHR(10)||MNS||CHR(10)||'COMMIT;' DDL_MINUS
FROM
(SELECT
FROM
(SELECT
'SELECT COUNT(1) , '''||TAB||''' TAB_NAME FROM ('||CHR(10)|| MN ||');' AS MNS
FROM
( SELECT LISTAGG(SEL_MINUS, chr(10)||' MINUS '||CHR(10))
FROM
( SELECT LISTAGG(SEL_MINUS, chr(10)||' MINUS '||CHR(10))
WITHIN GROUP (ORDER BY COL_ORD) MN, TAB
FROM
(SELECT
'SELECT '||LISTAGG(Column_Name,', ')
FROM
(SELECT
'SELECT '||LISTAGG(Column_Name,', ')
WITHIN GROUP (ORDER BY COLUMN_ID)||' FROM '|| OWNER ||'.'||TABLE_NAME SEL_MINUS,
OWNER ||'.'||TABLE_NAME TAB, 1 COL_ORD
FROM
( SELECT Column_Name,Column_Id ,OWNER,TABLE_NAME,
COUNT(column_id) OVER (PARTITION BY TABLE_NAME) CC, data_type
FROM SYS.ALL_TAB_COLUMNS
WHERE OWNER=UPPER('DM_ITI') AND table_name=UPPER('DIM_ARGOMENTO'
( SELECT Column_Name,Column_Id ,OWNER,TABLE_NAME,
COUNT(column_id) OVER (PARTITION BY TABLE_NAME) CC, data_type
FROM SYS.ALL_TAB_COLUMNS
WHERE OWNER=UPPER('DM_ITI') AND table_name=UPPER('DIM_ARGOMENTO'
ORDER BY 2)
GROUP BY OWNER,TABLE_NAME
UNION ALL
SELECT
'SELECT '||LISTAGG(Column_Name,',') WITHIN GROUP (ORDER BY COLUMN_ID)||' FROM '|| OWNER ||'.'||TABLE_NAME||'@DB_LINK' AS SEL_MINUS, OWNER ||'.'||TABLE_NAME TAB,
GROUP BY OWNER,TABLE_NAME
UNION ALL
SELECT
'SELECT '||LISTAGG(Column_Name,',') WITHIN GROUP (ORDER BY COLUMN_ID)||' FROM '|| OWNER ||'.'||TABLE_NAME||'@DB_LINK' AS SEL_MINUS, OWNER ||'.'||TABLE_NAME TAB,
2 COL_ORD
FROM
( SELECT Column_Name,Column_Id ,OWNER,TABLE_NAME,
COUNT(column_id) OVER (PARTITION BY TABLE_NAME) CC, data_type
FROM SYS.ALL_TAB_COLUMNS
WHERE OWNER=UPPER('DM_ITI') AND table_name=UPPER('DIM_ARGOMENTO')
ORDER BY 2)
GROUP BY OWNER,TABLE_NAME
)
GROUP BY TAB
));
( SELECT Column_Name,Column_Id ,OWNER,TABLE_NAME,
COUNT(column_id) OVER (PARTITION BY TABLE_NAME) CC, data_type
FROM SYS.ALL_TAB_COLUMNS
WHERE OWNER=UPPER('DM_ITI') AND table_name=UPPER('DIM_ARGOMENTO')
ORDER BY 2)
GROUP BY OWNER,TABLE_NAME
)
GROUP BY TAB
));
Questa una volta eseguita genera il seguente statement sql:
INSERT INTO DM_ITI.APPO_COUNT_DIFF
SELECT COUNT(1) , 'DM_ITI.DIM_ARGOMENTO' TAB_NAME
FROM (
SELECT TIPARGID, TIPARGDESCRIZIONE, TIPARGDATCAR
FROM
DM_ITI.DIM_ARGOMENTO
MINUS
SELECT TIPARGID,TIPARGDESCRIZIONE,TIPARGDATCAR
FROM
DM_ITI.DIM_ARGOMENTO@DB_LINK);
COMMIT;
------------------------------------------------------------------------------------
Una volta eseguito lo statement sql avremo:
In questo caso le due tabelle sono identiche.
-----------------------------------------------------------------------------------
Di seguito la DDL della tabella in cui inserire le count:
CREATE TABLE "DM_ITI"."APPO_COUNT_DIFF"
( "CC_DIFF" NUMBER,
"TAB_NAME" VARCHAR2(200 BYTE)
) ;
COMMENT ON COLUMN "DM_ITI"."APPO_COUNT_DIFF"."CC_DIFF" IS 'COUNT delle differenze = 0 Tabelle uguali >0 Tabelle differenti come contenuto';
COMMENT ON COLUMN "DM_ITI"."APPO_COUNT_DIFF"."TAB_NAME" IS 'Schema.Tabella indigata';
Nessun commento:
Posta un commento