Friday 8 February 2013

Sample Fastexport to export from multiple tables /SQL - Multiple exports in 1 Script

Generally for all Live Projects, we will find the requirement, where a Data file has to be created that contains information about Customer, His Behavior over the Websites, and the Account Details.

This can be easily solved using Fast-export. We can always use multiple SQL's to do this.

Just write these SQLs between the 
.Begin Export and .End Export

Example of Multi-SQL Fast-export script:


.logtable $LOADDB.LOGTABLE_ARRANGEMENT;



.run File $LOGON_FILE;
.begin export sessions 2;
.export outfile $VAR6 FORMAT TEXT MODE RECORD;
 
---- Below script is for all those records with product code as 006_DET  -----
 
SELECT TRIM(CAST(HEAD1.HEADER AS CHAR(100))) FROM
(
SELECT
'¿'||
TRIM(A.ID_HEADER)
|| '|' ||TRIM(A.CLUSTER_NAME)
|| '|' || TRIM(A.BUS_DATE)
|| '|' ||TRIM(A.PRODUCT_NAME)
|| '|' ||TRIM(A.PROCESS_NAME)
|| '|' ||TRIM(A.FREQUENCY_NAME)
|| '|' ||TRIM(A.VERSION_NO)
|| '¿'
AS HEADER
FROM
(
SELECT
'01' AS ID_HEADER
,TRIM(CLUSTER_NAME) AS CLUSTER_NAME
, CAST(CAST(CAST('$BUS_DATE' AS DATE FORMAT 'YYYY-MM-DD') AS DATE FORMAT 'DDMMMYYYY') AS VARCHAR(12)) AS BUS_DATE
,TRIM(PRODUCT_NAME) AS PRODUCT_NAME
,TRIM(PROCESS_NAME) AS PROCESS_NAME
,TRIM(FREQUENCY_NAME) AS FREQUENCY_NAME
,MAX(VERSION_NO)+1 AS VERSION_NO
FROM
?LOADDB.VERSION_HEAD
WHERE CAST('$BUS_DATE' AS DATE FORMAT 'YYYY-MM-DD')= RUNDATE
AND PRODUCT_NAME='PT_006_DET'
group by 1,2,3,4,5,6
) AS A
) AS HEAD1
;

----------------- This is for details listed under Accounts Table ----


Example of Fast-export:



.logtable $LOADDB.LOGTABLE_ARRANGEMENT;
.run File $LOGON_FILE;
.begin export sessions 2;
.export outfile $VAR6 FORMAT TEXT MODE RECORD;
 
 
SELECT TRIM(CAST(HEAD1.HEADER AS CHAR(100))) FROM
(
SELECT
'¿'||
TRIM(A.ID_HEADER)
|| '|' ||TRIM(A.CLUSTER_NAME)
|| '|' || TRIM(A.BUS_DATE)
|| '|' ||TRIM(A.PRODUCT_NAME)
|| '|' ||TRIM(A.PROCESS_NAME)
|| '|' ||TRIM(A.FREQUENCY_NAME)
|| '|' ||TRIM(A.VERSION_NO)
|| '¿'
AS HEADER
FROM
(
SELECT
'01' AS ID_HEADER
,TRIM(CLUSTER_NAME) AS CLUSTER_NAME
, CAST(CAST(CAST('$BUS_DATE' AS DATE FORMAT 'YYYY-MM-DD') AS DATE FORMAT 'DDMMMYYYY') AS VARCHAR(12)) AS BUS_DATE
,TRIM(PRODUCT_NAME) AS PRODUCT_NAME
,TRIM(PROCESS_NAME) AS PROCESS_NAME
,TRIM(FREQUENCY_NAME) AS FREQUENCY_NAME
,MAX(VERSION_NO)+1 AS VERSION_NO
FROM
?LOADDB.VERSION_HEAD
WHERE CAST('$BUS_DATE' AS DATE FORMAT 'YYYY-MM-DD')= RUNDATE
AND PRODUCT_NAME='PT_007_ACC'
group by 1,2,3,4,5,6
) AS A
) AS HEAD1
;

.end export;
.logoff;

Let us know if this was of help !!

Please donate for the renovation of old architectural buildings using the button on the right.


6 comments:

  1. Why there is that '¿' in sql?

    ReplyDelete
  2. By default, each row has 2 bytes of header information stored. The inverted question mark '¿' helps us to remove these special characters. It acts as a delimiter between data and header information. We can use unix commands to remove it once the file is generated.

    ReplyDelete
  3. Big Thx !!!
    Would it be any good idea of using FastExport to compare tables from different TD databases?

    ReplyDelete
  4. I would like to do "select * from testdb.testtable" and "select * from testdb2.testtable" and then diff

    ReplyDelete
    Replies
    1. Do this:
      Write your data from testdb.testtable into a file, after ordering the data based on some key values/columns.
      Follow the same process to write data from testdb2.testtable into another file, ordering your rows based on the same key values.
      Remember, the datatype of rows, order of fields, delimiter should be same.
      Then compare these files using the "diff" command in unix.

      Delete

Please share your thoughts and let us know the topics you want covered