Scenario:
We have requirement to create a package that can create output file dynamically from View/Table.
The columns of View/Table can change anytime but we don't want to open package and remap column/columns.
Solution:
Step1 :
Create the following variables as shown in picture, The values for the variables can be configured by using SSIS configuration,
This will give us freedom to change the name of file,directory path,Delemiter("," or "" etc) and file extension(txt or csv etc)
![]()
Step2:
After creating variable, Drag Execute SQL Task in control flow pane and configure as show in picture
![]()
![]()
Step3:
Drag Script task in Control Flow Pane and configure as show in picture
![]()
Step4:
Click on Edit Script button, paste following code and save the the changes and Finally OK button.
Note: I have "*" in the query , so if any new column/columns will be addeds to Table or view, my output file will get data for all the columns
and I don't have to change anything.
We have requirement to create a package that can create output file dynamically from View/Table.
The columns of View/Table can change anytime but we don't want to open package and remap column/columns.
Solution:
Step1 :
Create the following variables as shown in picture, The values for the variables can be configured by using SSIS configuration,
This will give us freedom to change the name of file,directory path,Delemiter("," or "" etc) and file extension(txt or csv etc)

Step2:
After creating variable, Drag Execute SQL Task in control flow pane and configure as show in picture


Step3:
Drag Script task in Control Flow Pane and configure as show in picture

Step4:
Click on Edit Script button, paste following code and save the the changes and Finally OK button.
/*
Microsoft SQL Server Integration Services Script Task
Write scripts using Microsoft Visual C# 2008.
The ScriptMain is the entry point class of the script.
*/
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
using System.Reflection;
namespace ST_367d705c54de4e9e9f890350f933c80b.csproj
{
[System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
#region VSTA generated code
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
public void Main()
{
OleDbDataAdapter A = new OleDbDataAdapter();
System.Data.DataTable dt = new System.Data.DataTable();
A.Fill(dt, Dts.Variables["User::VarObject"].Value);
string filePath = Dts.Variables["User::VarDirectoryPath"].Value.ToString() + Dts.Variables["User::VarFileName"].Value.ToString() + Dts.Variables["User::VarFileExtension"].Value.ToString();
int i = 0;
int RowCount = 0;
StreamWriter sw = null;
sw = new StreamWriter(filePath, false);
foreach (DataRow row in dt.Rows)
{
object[] array = row.ItemArray;
for (i = 0; i <>
{
sw.Write(array[i].ToString() + Dts.Variables["User::VarDelimeter"].Value.ToString());
}
sw.Write(array[i].ToString());
sw.WriteLine();
RowCount = RowCount + 1;
}
sw.Close();
Dts.TaskResult = (int)ScriptResults.Success;
}
}
}
Note: I have "*" in the query , so if any new column/columns will be addeds to Table or view, my output file will get data for all the columns
and I don't have to change anything.