3

I have to table in MySQL Server.

  1. Header Table. ╔════════════╦════════╦═════════════╦═════════════════╦══════════╗ ║ RecordType ║ CustID ║ DataGenDate ║ DataCreatedDate ║ SourceID ║ ╠════════════╬════════╬═════════════╬═════════════════╬══════════╣ ║ H ║ #1234 ║ 2018-01-05 ║ 2018-01-01 ║ V301 ║ ╚════════════╩════════╩═════════════╩═════════════════╩══════════╝

  2. Transaction Table ╔════════════╦══════════╦══════════════╦══════════════╦════════════╗ ║ RecordType ║ ProdCode ║ OpeningValue ║ ClosingValue ║ TranDate ║ ╠════════════╬══════════╬══════════════╬══════════════╬════════════╣ ║ T ║ AL001 ║ 95 ║ 90 ║ 2018-01-01 ║ ╠════════════╬══════════╬══════════════╬══════════════╬════════════╣ ║ T ║ AL002 ║ 54 ║ 40 ║ 2018-01-01 ║ ╠════════════╬══════════╬══════════════╬══════════════╬════════════╣ ║ T ║ AL003 ║ 63 ║ 43 ║ 2018-01-02 ║ ╠════════════╬══════════╬══════════════╬══════════════╬════════════╣ ║ T ║ AL004 ║ 56 ║ 23 ║ 2018-01-01 ║ ╚════════════╩══════════╩══════════════╩══════════════╩════════════╝

Header Table has Header Information and Transaction table have Transaction Data. I want a text file (vertical pipe separated "|") to be generated through SSIS in below format.

H|#1234|2018-01-05|2018-01-01|V301
----------------------------------------
T|AL001|95        |90        |2018-01-01
T|AL002|54        |40        |2018-01-01
T|AL003|63        |43        |2018-01-02
T|AL004|56        |23        |2018-01-01

I tried it with ole DB source and flat file destination to export the file but wasn't successful. only I am getting either transaction or Header Rows.

Thom A
  • 88,727
  • 11
  • 45
  • 75
Shahab Haidar
  • 625
  • 3
  • 11
  • 25
  • Not sure if you want this from MySQL, or SQL Server, (i suspect the latter), however, what you are asking for here is a dynamic Pivot. SSIS, however, cannot handle a dynamic pivot for exported data. SSIS **requires** static definitions of data. If you want that type of data in an export I would suggest using SSRS and a matrix. – Thom A Mar 07 '19 at 11:09
  • hi @Larnu I want this from SQL Server – Shahab Haidar Mar 07 '19 at 11:21
  • That doesn't change my above comments in regards to dynamic data though. – Thom A Mar 07 '19 at 11:33
  • You can use a C# script task to take a dataset and generate a .csv or Excel file dynamically. For Excel though you need to have the correct .dlls installed on the SSIS server. – Brad Mar 07 '19 at 12:51
  • Possible duplicate of [SSIS : Creating a flat file with different row formats](https://stackoverflow.com/questions/43286430/ssis-creating-a-flat-file-with-different-row-formats) – Chris Albert Mar 07 '19 at 17:29

1 Answers1

3

This can be done using a Script Task as follows, with C# used in this case. This will create a CSV file with the pipe (|) delimiter. Running a sample test with this I was able to import the output CSV file via an SSIS Flat File Connection Manager without any modifications to the file. This example assumes there's only a single row in the header table, otherwise you'll need to modify the SQL for this table to return the proper row.

using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Text;



//Windows Authentication (Integrated Security)
 string connectionString = @"Data Source=ServerName;Initial Catalog=DatabaseName;Integrated Security=true";
 string headerCmd = @"SELECT RecordType, CustID, DataGenDate, DataCreatedDate, SourceID FROM HeaderTable";
 string rowCmd = @"SELECT RecordType, CustID, DataGenDate, DataCreatedDate, SourceID FROM TransactionTable";

 string outputFile = Dts.Variables["User::FilePathVariable"].Value.ToString();

 StringBuilder csvData = new StringBuilder();
 int headerInt = 0;
 DataTable headerDT = new DataTable();
 DataTable rowDT = new DataTable();

 using (SqlConnection conn = new SqlConnection(connectionString))
 {
     SqlCommand headerSQL = new SqlCommand(headerCmd, conn);
     SqlCommand rowSQL = new SqlCommand(rowCmd, conn);

     SqlDataAdapter da = new SqlDataAdapter();

     conn.Open();

     //get header row
     da.SelectCommand = headerSQL;
     da.Fill(headerDT);

     //get data from Transaction table
     da.SelectCommand = rowSQL;
     da.Fill(rowDT);
 }

 //build header
 foreach (DataRow hDR in headerDT.Rows)
 {
     foreach (DataColumn hDC in headerDT.Columns)
     {
         csvData.Append(hDR[headerInt].ToString() + "|");
         headerInt++;
     }
 }

 //remove last pipe then start new line                    
 csvData.Remove(csvData.Length - 1, 1);
 csvData.Append(Environment.NewLine);

 //add rows
 foreach (DataRow rDR in rowDT.Rows)
 {
     for (int i = 0; i < headerInt; i++)
     {
         csvData.Append(rDR[i] + "|");
     }
     csvData.Remove(csvData.Length - 1, 1);
     csvData.Append(Environment.NewLine);
 }
 //write to CSV
 File.WriteAllText(outputFile, csvData.ToString());
userfl89
  • 4,610
  • 1
  • 9
  • 17
  • Why not building the header with a script task and importing data using a data flow task? – Hadi Mar 07 '19 at 18:19
  • 1
    @Hadi if the number of columns is static that would make sense. I wasn't sure if they were so I posted the answer to accommodate a varying number of columns. However if there's always 5 columns as indicated in the question using a script task to handle the columns and DFT for the import would simplify this. – userfl89 Mar 07 '19 at 18:24
  • 1
    I totally agree with that – Hadi Mar 07 '19 at 18:35