Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a possible way of exporting the tables of different structure to flat file using <code>Script Task</code>. This example will export two tables containing different fields and data to a flat file using Script Task. In order to export the data, you can use the <code>DataReader</code> instead of using the <code>DataGrid</code>. There could be other possible ways to do this. </p> <p><strong>Step-by-step process:</strong></p> <ol> <li>Create three tables named <code>dbo.TablesList</code>, <code>dbo.Source1</code> and <code>dbo.Source2</code> using the scripts given under <strong>SQL Scripts</strong> section.</li> <li>Populate the tables <code>dbo.TablesList</code>, <code>dbo.Source1</code> and `dbo.Source2`` with data shown in screenshot #<strong>1</strong>.</li> <li>On the SSIS package's <code>Connection manager</code>, create an <code>OLE DB connection</code> named <strong>SQLServer</strong> to connect to the SQL Server instance as shown in screenshot #<strong>2</strong>.</li> <li>In the package, create 4 variables as shown in screenshot #<strong>3</strong>.</li> <li>In the Control Flow, place an <code>Execute SQL Task</code>, a <code>Foreach Loop Container</code> and a <code>Script Task</code> within the <code>Foreach loop container</code> as shown in screenshot #<strong>4</strong>.</li> <li>Configure the <code>Execute SQL task</code> as shown in screenshots #<strong>5</strong> and #<strong>6</strong>.</li> <li>Configure the <code>Foreach Loop container</code> as shown in screenshots #<strong>7</strong> and #<strong>8</strong>.</li> <li>Replace the Main method inside the Script Task with the code given under the section <code>Script Task Code</code>.</li> <li>Screenshot #<strong>9</strong> shows package execution.</li> <li>Screenshots #<strong>10</strong> - #<strong>12</strong> show the files exported from SSIS using Script Task code.</li> </ol> <p>Hope that helps.</p> <p><strong>SQL Scripts:</strong></p> <pre><code>CREATE TABLE [dbo].[Source1]( [Id] [int] IDENTITY(1,1) NOT NULL, [ItemNumber] [varchar](20) NOT NULL, [ItemName] [varchar](50) NOT NULL, CONSTRAINT [PK_Source1] PRIMARY KEY CLUSTERED ([Id] ASC)) ON [PRIMARY] GO CREATE TABLE [dbo].[Source2]( [Id] [int] IDENTITY(1,1) NOT NULL, [Country] [varchar](20) NOT NULL, [StateProvince] [varchar](50) NOT NULL, CONSTRAINT [PK_Source2] PRIMARY KEY CLUSTERED ([Id] ASC)) ON [PRIMARY] GO CREATE TABLE [dbo].[TablesList]( [Id] [int] IDENTITY(1,1) NOT NULL, [TableName] [varchar](50) NOT NULL, [FilePath] [varchar](255) NOT NULL, CONSTRAINT [PK_Tables] PRIMARY KEY CLUSTERED ([Id] ASC)) ON [PRIMARY] GO </code></pre> <p><strong>Script Task Code:</strong> (Use the code given below to replace the Main() method in your Script task)</p> <p><strong>VB</strong> Main() method code that can be used in <strong><code>SSIS 2005 and above</code></strong>:</p> <pre><code>Public Sub Main() Dim varCollection As Variables = Nothing Dts.VariableDispenser.LockForRead("User::TableName") Dts.VariableDispenser.LockForRead("User::FileName") Dts.VariableDispenser.LockForRead("User::Delimiter") Dts.VariableDispenser.GetVariables(varCollection) Dim fileName As String = varCollection("User::FileName").Value.ToString() Dim query As String = "SELECT * FROM " &amp; varCollection("User::TableName").Value.ToString() Dim delimiter As String = varCollection("User::Delimiter").Value.ToString() Dim writer As StreamWriter = Nothing Dim connection As OleDbConnection = New OleDbConnection(Dts.Connections("SQLServer").ConnectionString) Dim command As OleDbCommand = Nothing Dim reader As OleDbDataReader = Nothing Try If File.Exists(fileName) Then File.Delete(fileName) End If connection.Open() command = New OleDbCommand(query, connection) reader = command.ExecuteReader() If reader.HasRows Then writer = New System.IO.StreamWriter(fileName) Dim row As Integer = 0 While reader.Read() Dim header As Integer = 0 Dim counter As Integer = 0 Dim fieldCount As Integer = reader.FieldCount - 1 If row = 0 Then While header &lt;= fieldCount If header &lt;&gt; fieldCount Then writer.Write(reader.GetName(header).ToString() &amp; delimiter) Else writer.WriteLine(reader.GetName(header).ToString()) End If header += 1 End While End If While counter &lt;= fieldCount If counter &lt;&gt; fieldCount Then writer.Write(reader(counter).ToString() &amp; delimiter) Else writer.WriteLine(reader(counter).ToString()) End If counter += 1 End While row += 1 End While End If Catch ex As Exception Throw ex Finally connection.Close() writer.Close() End Try Dts.TaskResult = ScriptResults.Success End Sub </code></pre> <p><strong>Screenshot #1:</strong></p> <p><img src="https://i.stack.imgur.com/Ztby4.png" alt="1"></p> <p><strong>Screenshot #2:</strong></p> <p><img src="https://i.stack.imgur.com/C7P38.png" alt="2"></p> <p><strong>Screenshot #3:</strong></p> <p><img src="https://i.stack.imgur.com/YfsST.png" alt="3"></p> <p><strong>Screenshot #4:</strong></p> <p><img src="https://i.stack.imgur.com/ufiEM.png" alt="4"></p> <p><strong>Screenshot #5:</strong></p> <p><img src="https://i.stack.imgur.com/NbCxm.png" alt="5"></p> <p><strong>Screenshot #6:</strong></p> <p><img src="https://i.stack.imgur.com/0GjNS.png" alt="6"></p> <p><strong>Screenshot #7:</strong></p> <p><img src="https://i.stack.imgur.com/werHc.png" alt="7"></p> <p><strong>Screenshot #8:</strong></p> <p><img src="https://i.stack.imgur.com/R3Q7P.png" alt="8"></p> <p><strong>Screenshot #9:</strong></p> <p><img src="https://i.stack.imgur.com/NinZL.png" alt="9"></p> <p><strong>Screenshot #10:</strong></p> <p><img src="https://i.stack.imgur.com/J7Ly6.png" alt="10"></p> <p><strong>Screenshot #11:</strong></p> <p><img src="https://i.stack.imgur.com/qM52Y.png" alt="11"></p> <p><strong>Screenshot #12:</strong></p> <p><img src="https://i.stack.imgur.com/HH9JB.png" alt="12"></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload