REPLACE MACRO .BTEQ_EXPORT
(
DATABASENAME VARCHAR(100),
TABLENAME VARCHAR(100),
FILENAME VARCHAR(500),
SERVER VARCHAR(100) default '' ,
USERNAME VARCHAR(300) default '',
PASS VARCHAR(15) default 'password'

)
AS
(

SEL
'                    
 .SESSIONS 5
 .SET ECHOREQ ON
.SET FORMAT OFF
.SET QUIET ON
.SET WIDTH 1000
.SET RETLIMIT 1000000
.SET RETCANCEL ON
.LOGON  '   || :SERVER || '/' || :USERNAME || ', ' || :PASS || '; '  ||     '
.EXPORT REPORT FILE =' || :FILENAME BTEQ_SCRIPT
    FROM SYS_CALENDAR.CALENDAR
WHERE DATE=CALENDAR_DATE
   
  UNION
  SEL '                   SEL TOP 1000000            '
  FROM SYS_CALENDAR.CALENDAR
WHERE DATE=CALENDAR_DATE
 
  UNION
 
  SEL   '                 TRIM(COALESCE (CAST(' || COLUMNNAME ||  'AS VARCHAR(300)),' ||'27'xc || '27'xc || ')) ||  ' ||'27'xc ||'|' || '27'xc || '||'
FROM DBC.COLUMNS
WHERE TABLENAME =:TABLENAME
AND DATABASENAME =:DATABASENAME
AND COLUMNNAME NOT LIKE ANY ('%SSN%','%ACCNO%','%ACCT_NO%')
UNION
SEL  '                  CAST('
FROM SYS_CALENDAR.CALENDAR
WHERE DATE=CALENDAR_DATE
   
 UNION

 SEL '               FROM ' || :DATABASENAME || '.' || :TABLENAME   || ' ;'
 FROM SYS_CALENDAR.CALENDAR
WHERE DATE=CALENDAR_DATE

UNION
SEL '                AS VARCHAR(1000))           '
 FROM SYS_CALENDAR.CALENDAR
WHERE DATE=CALENDAR_DATE

 UNION

 SEL '.EXPORT RESET .LOGOFF'
 FROM SYS_CALENDAR.CALENDAR
WHERE DATE=CALENDAR_DATE
;
);
--------------------------------------------------------------------------------
REPLACE MACRO SEL_VW_MAP --_1Ai
( DB VARCHAR(100), TB VARCHAR(100))
AS
(
SELECT HEAD
FROM
(SEL CAST(1 AS DECIMAL(13,0)) AS ROWID1 ,' REPLACE VIEW VW' || :DB||  '.' || :TB  || '  AS LOCKING ROW FOR ACCESS  SEL '  AS HEAD
FROM SYS_CALENDAR.CALENDAR
WHERE CALENDAR_DATE=DATE
UNION
SEL COLUMNID,CASE WHEN COLUMNID= MAXID THEN COLUMNNAME ELSE COLUMNNAME || ',' END AS DETAILS
FROM DBC.COLUMNS A
JOIN
(
SEL MAX(COLUMNID) AS MAXID,TABLENAME AS TBN ,DATABASENAME AS DBN   FROM DBC.COLUMNS  GROUP BY 2,3
) M
ON A.TABLENAME =M.TBN
AND A.DATABASENAME=M.DBN
WHERE DATABASENAME=:DB
AND TABLENAME=:TB
/*AND TABLENAME NOT LIKE 'STG_EM_%'
AND TABLENAME NOT LIKE 'STG_TMP_%'
AND TABLENAME NOT LIKE 'STG_W_IM_%'*/
UNION
SEL 9999999,'FROM ' || :DB || '.' || :TB || ';' AS TAIL
FROM SYS_CALENDAR.CALENDAR
WHERE CALENDAR_DATE=DATE
) S
ORDER BY ROWID1
;
);
--------------------------------------------------------------------------------
REPLACE MACRO .J_TABLESIZE (DATABASENAME VARCHAR(100),TABLENAME VARCHAR(100))
AS
(
SEL DATABASENAME,TABLENAME,SUM(CURRENTPERM)/(1024.0*1024.0) SPACE_IN_MB
,SUM(CURRENTPERM)/(1024.0*1024.0*1024.0) SPACE_IN_GB ,MAX(CURRENTPERM) *144*1.0 /(1024*1024.0) EFFECTIVE_SPACE_IN_ODIN_IN_MB
FROM DBC.TABLESIZE

WHERE
DATABASENAME =:DATABASENAME
AND
TABLENAME =:TABLENAME
GROUP BY 1,2
;
) ;
--------------------------------------------------------------------------------
REPLACE MACRO col_chk
       (
       db CHAR(30)
       ,tbl CHAR(30)
       )
       AS
    
     (
     SELECT
    
     columnid,
     ',sum(case when ' || columnname || ' is null then 0 else 1 end) * 100.00 /row_cnt as ' || columnname
     FROM dbc.COLUMNS
     WHERE databasename = :db
     AND tablename = :tbl
    
     UNION
    
     SELECT 1, 'select sum(1) as row_cnt'
     FROM sys_calendar.calendar
     WHERE calendar_date = CURRENT_DATE
    
     UNION
    
     SELECT 19999, 'from ' || databasename || '.' || tablename
     FROM dbc.COLUMNS
     WHERE databasename = :db
     AND tablename = :tbl
     ORDER BY 1
     ;
     );
--------------------------------------------------------------------------------
REPLACE MACRO .SEL_VW_MAP --_1Ai
( DB VARCHAR(100), TB VARCHAR(100))
AS
(
SEL CAST(1 AS DECIMAL(13,0)),' REPLACE VIEW VW' || :DB||  '.' || :TB  || '  AS LOCKING ROW FOR ACCESS  SEL '
FROM SYS_CALENDAR.CALENDAR
WHERE CALENDAR_DATE=DATE
UNION
SEL COLUMNID,COLUMNNAME || ','
FROM DBC.COLUMNS
WHERE DATABASENAME=:DB
AND TABLENAME=:TB
AND TABLENAME NOT LIKE 'STG_EM_%'
AND TABLENAME NOT LIKE 'STG_TMP_%'
AND TABLENAME NOT LIKE 'STG_W_IM_%'
UNION
SEL 9999999,'FROM ' || :DB || '.' || :TB || ';'
FROM SYS_CALENDAR.CALENDAR
WHERE CALENDAR_DATE=DATE
;
);
--------------------------------------------------------------------------------
REPLACE MACRO .BTEQ_EXPORT1
(
DATABASENAME VARCHAR(100),
TABLENAME VARCHAR(100),
FILENAME VARCHAR(500),
SERVER VARCHAR(100) default '' ,
USERNAME VARCHAR(300) default '',
PASS VARCHAR(15) default 'password'
)
AS
(
SEL
'                    
 .SESSIONS 5
.SET ECHOREQ ON
.SET FORMAT OFF
.SET QUIET ON
.SET WIDTH 1000
.SET RETLIMIT 1000000
.SET RETCANCEL ON
.LOGON  '   || :SERVER || '/' || :USERNAME || ', ' || :PASS || '; '  ||     '
.EXPORT REPORT FILE =' || :FILENAME BTEQ_SCRIPT
    FROM SYS_CALENDAR.CALENDAR
WHERE DATE=CALENDAR_DATE
 UNION
  SEL '                   SEL TOP 1000000            '
  FROM SYS_CALENDAR.CALENDAR
  WHERE DATE=CALENDAR_DATE
  UNION
  SEL   '                 TRIM(COALESCE (CAST(' || COLUMNNAME ||  'AS VARCHAR(300)),' ||'27'xc || '27'xc || ')) ||  ' ||'27'xc ||'|' || '27'xc || '||'
FROM DBC.COLUMNS
WHERE TABLENAME =:TABLENAME
AND DATABASENAME =:DATABASENAME
AND COLUMNNAME NOT LIKE ANY ('%SSN%','%ACCNO%','%ACCT_NO%')
UNION
SEL  '                  CAST('
FROM SYS_CALENDAR.CALENDAR
WHERE DATE=CALENDAR_DATE

 UNION
 SEL '               FROM ' || :DATABASENAME || '.' || :TABLENAME   || ' ;'
FROM SYS_CALENDAR.CALENDAR
WHERE DATE=CALENDAR_DATE
UNION
SEL '                AS VARCHAR(1000))           '
FROM SYS_CALENDAR.CALENDAR
WHERE DATE=CALENDAR_DATE
 UNION
 SEL '.EXPORT RESET .LOGOFF'
FROM SYS_CALENDAR.CALENDAR
WHERE DATE=CALENDAR_DATE
;
);
--------------------------------------------------------------------------------
REPLACE MACRO .SEL_VW_MAP_1Ai
( DB VARCHAR(100), TB VARCHAR(100))
AS
(
SEL CAST(1 AS DECIMAL(13,0)),' REPLACE VIEW VW' || :DB||  '.' || :TB  || '  AS LOCKING ROW FOR ACCESS  SEL '
FROM SYS_CALENDAR.CALENDAR
WHERE CALENDAR_DATE=DATE
UNION
SEL COLUMNID,COLUMNNAME || ','
FROM DBC.COLUMNS
WHERE DATABASENAME=:DB
AND TABLENAME=:TB
UNION
SEL 9999999,'FROM ' || :DB || '.' || :TB || ';'
FROM SYS_CALENDAR.CALENDAR
WHERE CALENDAR_DATE=DATE
;
);
--------------------------------------------------------------------------------
REPLACE MACRO .J_DATABASE_OCCUPIED_SPACE (DATABASENAME VARCHAR(100))
AS
(
SEL DATABASENAME,TABLENAME,SUM(CURRENTPERM)/(1024.0*1024.0) SPACE_IN_MB
,SUM(CURRENTPERM)/(1024.0*1024.0*1024.0) SPACE_IN_GB ,MAX(CURRENTPERM) *MF*1.0 /(1024*1024.0) EFFECTIVE_SPACE_IN_ODIN_IN_MB
FROM DBC.TABLESIZE
,
(SEL COUNT(DISTINCT VPROC) MF FROM DBC.TABLESIZE) X
WHERE
DATABASENAME =:DATABASENAME
/*AND
TABLENAME =:TABLENAME
*/GROUP BY 1,2
;
) ;
--------------------------------------------------------------------------------
REPLACE MACRO .DATABASE_COMPARISON
(DEV_DB VARCHAR(100)
,SIT_DB VARCHAR(100)
)
AS
(
SEL ' Following Talbe Could not be found on SIT schema but present in dev scehma <Ignore if nothing follows >'
;
SEL A.TABLENAME
FROM DBC.TABLES A
WHERE
A.DATABASENAME=:DEV_DB
AND TABLEKIND='T'
AND A.TABLENAME NOT IN
(SEL A.TABLENAME
FROM DBC.TABLES
WHERE DATABASENAME=:SIT_DB
AND TABLEKIND='T')
ORDER BY 1
;
  SEL ' Following Talbe Could not be found on DEV schema but present in SIT scehma  <Ignore if nothing follows > ' ;
 
  SEL A.TABLENAME
FROM DBC.TABLES A
WHERE
A.DATABASENAME=:SIT_DB
AND TABLEKIND='T'
AND A.TABLENAME NOT IN
(SEL A.TABLENAME
FROM DBC.TABLES
WHERE DATABASENAME=:DEV_DB
AND TABLEKIND='T')
ORDER BY 1
;
SEL 'Column Level Discrepancies <Ignore if nothing follows >';

(SEL
'DEV',A.TABLENAME ,A.COLUMNNAME,
CASE
WHEN A.COLUMNTYPE ='I' THEN 'INTEGER'
WHEN A.COLUMNTYPE='D' THEN 'DECIMAL'
WHEN A.COLUMNTYPE='CF' THEN 'CHAR'
WHEN A.COLUMNTYPE='CV' THEN 'VARCHAR'
WHEN A.COLUMNTYPE='DA' THEN 'DATE'
WHEN A.COLUMNTYPE='I8' THEN 'BIGINT'
ELSE A.COLUMNTYPE
END  AS DATATYPE
,A.COLUMNLENGTH
FROM DBC.COLUMNS A
WHERE
A.DATABASENAME=:DEV_DB
AND
TABLENAME NOT IN
(
SEL A.TABLENAME
FROM DBC.TABLES A
WHERE
A.DATABASENAME=:DEV_DB
--AND TABLEKIND='T'
AND A.TABLENAME NOT IN
(SEL A.TABLENAME
FROM DBC.TABLES
WHERE DATABASENAME=:SIT_DB
--AND TABLEKIND='T')
)
)
MINUS
SEL
'DEV',A.TABLENAME,A.COLUMNNAME,
CASE
WHEN A.COLUMNTYPE ='I' THEN 'INTEGER'
WHEN A.COLUMNTYPE='D' THEN 'DECIMAL'
WHEN A.COLUMNTYPE='CF' THEN 'CHAR'
WHEN A.COLUMNTYPE='CV' THEN 'VARCHAR'
WHEN A.COLUMNTYPE='DA' THEN 'DATE'
WHEN A.COLUMNTYPE='I8' THEN 'BIGINT'
ELSE A.COLUMNTYPE
END DATATYPE,
A.COLUMNLENGTH
FROM DBC.COLUMNS A
WHERE
A.DATABASENAME=:SIT_DB
)
UNION
(
SEL
'SIT',A.TABLENAME SIT_tABL,A.COLUMNNAME,
CASE
WHEN A.COLUMNTYPE ='I' THEN 'INTEGER'
WHEN A.COLUMNTYPE='D' THEN 'DECIMAL'
WHEN A.COLUMNTYPE='CF' THEN 'CHAR'
WHEN A.COLUMNTYPE='CV' THEN 'VARCHAR'
WHEN A.COLUMNTYPE='DA' THEN 'DATE'
WHEN A.COLUMNTYPE='I8' THEN 'BIGINT'
ELSE A.COLUMNTYPE
END  AS DATATYPE
,A.COLUMNLENGTH
FROM DBC.COLUMNS A
WHERE
A.DATABASENAME=:SIT_DB
MINUS
SEL
'SIT' ,A.TABLENAME,A.COLUMNNAME,
CASE
WHEN A.COLUMNTYPE ='I' THEN 'INTEGER'
WHEN A.COLUMNTYPE='D' THEN 'DECIMAL'
WHEN A.COLUMNTYPE='CF' THEN 'CHAR'
WHEN A.COLUMNTYPE='CV' THEN 'VARCHAR'
WHEN A.COLUMNTYPE='DA' THEN 'DATE'
WHEN A.COLUMNTYPE='I8' THEN 'BIGINT'
ELSE A.COLUMNTYPE
END DATATYPE,
A.COLUMNLENGTH
FROM DBC.COLUMNS A
WHERE
A.DATABASENAME=:DEV_DB
)
ORDER BY 2,3,1
;


);

Comments

Popular posts from this blog

Macro to generate Mload Script

Finding Skewed Tables in Teradata

The Secret behind A B I N I T I O.