Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The process that you are trying to achieve in the question can be done with the help of the <code>Output Columns</code> in <code>Script Component Transformation</code> task instead of using the variables. The below example explains how this can be achieved and also it demonstrates how variables can be used within the script component. However the same functionality can be achieved using <code>Derived Column Transformation</code> task as well. </p> <p>This example reads a CSV file and based on the first column value, it will extract value from second column starting from a give position and length. All these values will be stored in a variable.</p> <p><strong>Step-by-step process:</strong></p> <ol> <li><p>Create a CSV file as shown in screenshot #<strong>1</strong>. If the first column has the value <code>Customer</code>, we will extract <strong>6</strong> characters from <strong>9th</strong> character on the second column value. If the first column has the value <code>Vendor</code>, we will extract <strong>7</strong> characters from <strong>21st</strong> character on the second column value. For every other value, the package will assume value zero for second column value.</p></li> <li><p>Create a table named <code>dbo.Destination</code> using the script under <strong>SQL Scripts</strong> section. The package will insert the CSV data into this table.</p></li> <li><p>On the SSIS package, create <strong>6</strong> variables as shown in screenshot #<strong>2</strong>. Configure an <code>OLE DB connection</code> to connect to the SQL Server instance. Configure a <code>Flat File connection</code> to read the CSV file as shown in screenshots #<strong>3</strong> - #<strong>6</strong>. Also, place a <code>Data Flow Task</code> on the <code>Control Flow</code> tab of the package. </p></li> <li><p>Configure the Data Flow Tab with a <code>Flat File Source</code>, <code>Script Component Transformation Task</code> (Refer screenshot #<strong>7</strong>) and an <code>OLE DB Destination</code>.</p></li> <li><p>Configure the <code>Flat File Source</code> task as shown in screenshots #<strong>8</strong> and #<strong>9</strong>. </p></li> <li><p>Configure the <code>Script Component</code> transformation task as shown in screenshots #<strong>10</strong> and #<strong>11</strong>. A new <code>Output Column</code> named <strong>SNumber</strong> of data type <code>four-byte signed integer [DT_I4]</code> is created to store the extracted value. Replace the <code>Script Component</code> code with code displayed under <code>Script Component Code</code> section. <code>PreExecute</code> method reads the package variables' values and the method <code>Input0_ProcessInputRow</code> processes the logic to populate the Output Column <code>SNumber</code>.</p></li> <li><p>Configure the <code>OLE DB Destination</code> task as shown in screenshots #<strong>12</strong> and #<strong>13</strong>.</p></li> <li><p>Screenshot #<strong>14</strong> shows sample package execution.</p></li> <li><p>Screenshot #<strong>15</strong> shows the data in the table <code>dbo.Destination</code> after the package execution.</p></li> </ol> <p>Hope that helps.</p> <p><strong>SQL Scripts:</strong> .</p> <pre><code>CREATE TABLE [dbo].[Destination]( [Id] [int] IDENTITY(1,1) NOT NULL, [Header] [varchar](50) NOT NULL, [Value] [varchar](50) NOT NULL, [SNumber] [int] NOT NULL, CONSTRAINT [PK_Destination] PRIMARY KEY CLUSTERED ([Id] ASC)) ON [PRIMARY] GO </code></pre> <p><strong>Script Component Code:</strong></p> <p><strong>C#</strong> code that can be used only in <strong><code>SSIS 2008 and above</code></strong>. .</p> <pre><code>/* Microsoft SQL Server Integration Services Script Component * Write scripts using Microsoft Visual C# 2008. * ScriptMain is the entry point class of the script.*/ using System; using System.Data; using Microsoft.SqlServer.Dts.Pipeline.Wrapper; using Microsoft.SqlServer.Dts.Runtime.Wrapper; [Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute] public class ScriptMain : UserComponent { IDTSVariables100 varCollection = null; string customer = string.Empty; int customerPosition = 0; int customerLength = 0; string vendor = string.Empty; int vendorPosition = 0; int vendorLength = 0; public override void PreExecute() { this.VariableDispenser.LockForRead("User::Customer"); this.VariableDispenser.LockForRead("User::CustomerPosition"); this.VariableDispenser.LockForRead("User::CustomerLength"); this.VariableDispenser.LockForRead("User::Vendor"); this.VariableDispenser.LockForRead("User::VendorPosition"); this.VariableDispenser.LockForRead("User::VendorLength"); this.VariableDispenser.GetVariables(out varCollection); customer = varCollection["User::Customer"].Value.ToString(); customerPosition = Convert.ToInt32(varCollection["User::CustomerPosition"].Value); customerLength = Convert.ToInt32(varCollection["User::CustomerLength"].Value); vendor = varCollection["User::Vendor"].Value.ToString(); vendorPosition = Convert.ToInt32(varCollection["User::VendorPosition"].Value); vendorLength = Convert.ToInt32(varCollection["User::VendorLength"].Value); base.PreExecute(); } public override void PostExecute() { base.PostExecute(); } public override void Input0_ProcessInputRow(Input0Buffer Row) { if (Row.Header.ToString().Trim() == customer) { Row.SNumber = Convert.ToInt32(Row.Value.Substring(customerPosition, customerLength)); } else if (Row.Header.ToString().Trim() == vendor) { Row.SNumber = Convert.ToInt32(Row.Value.Substring(vendorPosition, vendorLength)); } else { Row.SNumber = 0; } } } </code></pre> <p><strong>Screenshot #1:</strong></p> <p><img src="https://i.stack.imgur.com/hHRzj.png" alt="1"></p> <p><strong>Screenshot #2:</strong></p> <p><img src="https://i.stack.imgur.com/a2tY2.png" alt="2"></p> <p><strong>Screenshot #3:</strong></p> <p><img src="https://i.stack.imgur.com/YiDfI.png" alt="3"></p> <p><strong>Screenshot #4:</strong></p> <p><img src="https://i.stack.imgur.com/p1OyP.png" alt="4"></p> <p><strong>Screenshot #5:</strong></p> <p><img src="https://i.stack.imgur.com/ad42L.png" alt="5"></p> <p><strong>Screenshot #6:</strong></p> <p><img src="https://i.stack.imgur.com/CieO9.png" alt="6"></p> <p><strong>Screenshot #7:</strong></p> <p><img src="https://i.stack.imgur.com/bkukG.png" alt="7"></p> <p><strong>Screenshot #8:</strong></p> <p><img src="https://i.stack.imgur.com/HM0yJ.png" alt="8"></p> <p><strong>Screenshot #9:</strong></p> <p><img src="https://i.stack.imgur.com/uHbBE.png" alt="9"></p> <p><strong>Screenshot #10:</strong></p> <p><img src="https://i.stack.imgur.com/HJsZK.png" alt="10"></p> <p><strong>Screenshot #11:</strong></p> <p><img src="https://i.stack.imgur.com/Jhy56.png" alt="11"></p> <p><strong>Screenshot #12:</strong></p> <p><img src="https://i.stack.imgur.com/OqAcN.png" alt="12"></p> <p><strong>Screenshot #13:</strong></p> <p><img src="https://i.stack.imgur.com/ToI9o.png" alt="13"></p> <p><strong>Screenshot #14:</strong></p> <p><img src="https://i.stack.imgur.com/cQhJc.png" alt="14"></p> <p><strong>Screenshot #15:</strong></p> <p><img src="https://i.stack.imgur.com/gm7WH.png" alt="15"></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. 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.
    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