How to create Text or CSV File Dynamically from Table or View in SSIS Package by using Script Task - SSIS Tutorial

Scenario: Download Script

You are working as SQL Server Integration Services developer, You are asked to create an SSIS Package that should get the data from table or view and create flat file with date-time. But there is catch, the definition of table or view can change anytime. Your Package should be able to create flat file dynamically.


As the number of columns can change anytime for Table or view from which we are extracting data, we can not really use the built-in Source and Destination. If we do, we have to remap columns every time table or view definition change. 

We are going to use Script Task and handle the situation by using C# inside Script Task.

Step 1: Create new SSIS Package and create Variables 
Open your SSDT ( SQL Server Data Tools) and create new SSIS Package. Once done, create below variables as shown.

FlatFileNamePart: Provide the file name that you would like to create. Date-time part will be added to it.
DestinationFolder: Provide the destination folder path where you would like to create your files.
TableName: Provide the table name with schema from which you like to get data
LogFolder: Provide the Log folder path where you would like to create log file in case of error in Script Task.
FileDelimiter : Provide the file delimiter you would like to use, you can provide comma, pipe etc.
FileExtension: Provide the extension for your file such as txt or csv etc.

Create variables in SSIS Package to generate flat file dynamically from table or view

Step 2: Create ADO.NET Connection in SSIS Package to use in Script Task
Create ADO.NET Connection Manager so we can use in Script Task to get table or view  data to export to flat file. This ADO.Net connection should be pointing to Database which contains the table from which we want to export data to flat file.
Create ADO.NET Connection in SSIS Package to use in Script Task to export data from table or view to text/csv file dynamically in SSIS Package

Step3: Add Variables to Script Task to use from SSIS Package
Bring the Script Task on Control Flow Pane in SSIS Package and open by double clicking Check-box in front of variable to add to Script Task.

Add variables to Script Task to generate flat file dynamically from SQL Server Table or View in SSIS Package

Step 4: Add Script to Script task Editor in SSIS Package to Export Data from SQL Server Table or View to flat file dynamically.

Click Edit Button and it will open Script Task Editor.
Under #region Namespaces, I have added below code

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

Under public void Main() { 
I have added below code.

            string datetime = DateTime.Now.ToString("yyyyMMddHHmmss");

                //Declare Variables
      string FileNamePart = Dts.Variables["User::FlatFileNamePart"].Value.ToString();
      string DestinationFolder = Dts.Variables["User::DestinationFolder"].Value.ToString();
      string TableName = Dts.Variables["User::TableName"].Value.ToString();
      string FileDelimiter = Dts.Variables["User::FileDelimiter"].Value.ToString();
      string FileExtension = Dts.Variables["User::FileExtension"].Value.ToString();

                //USE ADO.NET Connection from SSIS Package to get data from table
                SqlConnection myADONETConnection = new SqlConnection();
                myADONETConnection = (SqlConnection)(Dts.Connections["DBConn"].AcquireConnection(Dts.Transaction) as SqlConnection);

                //Read data from table or view to data table
                string query = "Select * From "+TableName;
                SqlCommand cmd = new SqlCommand(query, myADONETConnection);
                DataTable d_table = new DataTable();

              string FileFullPath = DestinationFolder +"\\"+ FileNamePart +"_" + datetime + FileExtension;

                        StreamWriter sw = null;
                        sw = new StreamWriter(FileFullPath, false);

                        // Write the Header Row to File
                        int ColumnCount = d_table.Columns.Count;
                        for (int ic = 0; ic < ColumnCount; ic++)
                            if (ic < ColumnCount - 1)

                        // Write All Rows to the File
                        foreach (DataRow dr in d_table.Rows)
                            for (int ir = 0; ir < ColumnCount; ir++)
                                if (!Convert.IsDBNull(dr[ir]))
                                if (ir < ColumnCount - 1)
                    Dts.TaskResult = (int)ScriptResults.Success;


            catch (Exception exception)

                // Create Log File for Errors
                using (StreamWriter sw = File.CreateText(Dts.Variables["User::LogFolder"].Value.ToString() + "\\" +
                    "ErrorLog_" + datetime + ".log"))
                    Dts.TaskResult = (int)ScriptResults.Failure;


Step 5: Save the script and run the Package
Save the script in Script Task editor and then run your SSIS Package. It should create the flat file with datetime every time you will execute package. You have freedom to change the extension of file such as txt or csv. Also you can change delimiter by changing value of variable.

I execute package couple of times and it generated files as per variable values. Also I dropped the column in source table and view and tested. It was able to generate file dynamically without making any change to package.

How to create flat file dynamically from SQL Server Table or View in SSIS Package

Video Demo: How to create flat file dynamically from Table or View in SSIS Paclage

 Related Posts / Videos on Dynamic Text / CSV files by Script Task 


  1. Great tutorial. Thank you for sharing

  2. This is amazing! However, i need to wright 200m+ rows to the file.....can i do it in batches?

  3. I love you guys. I've learned a bunch from you. On this particular tutorial I am trying to create a tab delimited text file. What would the delimiter be in the variable? I am then reloading these files in a system through SSIS. I can get the .csv file to work but not the tab delimited text file. Any ideas? I thought the delimiter would be \t but that doesn't work.

    1. add this after declaring your variables

      FileDelimiter = FileDelimiter.Replace("\\t", "\t");

  4. Hi, great tutorial.
    I would be interested in changing the dafult date format and length of text fields as instead of DATE your script seem to export datetime and instead of cut strings it exports full lenght strings.
    Any suggestions to that?