How can we split the rows using Script Component transformation

We need to split the records according to Seats

Sample records:



















1) Use Data Flow Task
    a) Oledb Source
    b)Script component transformation
    c) Row sampling










































Script to Split the Seats in  row wise

/*
 To split the rows with startValue and endValue and to store in Sql Server

*/

using System;
using System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using System.IO;
using System.Text ;
using System.Text.RegularExpressions;
using System.Windows.Forms;

[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{

    public override void PreExecute()
    {
        base.PreExecute();
        /*
          Add your code here for preprocessing or remove if not needed
        */
    }

    public override void PostExecute()
    {
        base.PostExecute();
        /*
          Add your code here for postprocessing or remove if not needed
          You can set read/write variables here, for example:
          Variables.MyIntVar = 100
        */
    }

 

    public override void Input0_ProcessInputRow(Input0Buffer Row)
    {

        string sPattern = "to";
        Regex reg = new Regex(sPattern, RegexOptions.IgnoreCase); // for regular expressions we need to add namespace using System.Text.RegularExpressions;
       
        var mString =Row.Seats;
        Match mMatch = reg.Match(mString);  //checking for match with pattern and String

        string FirstString="";
        string LastString="";

        int FirstLength=0;
        int LastLength=0;
       

        int startSeat = 0;
        int endSeat = 0;

        if (mMatch.Success)
        {
            FirstLength = mString.IndexOf(sPattern);  // give the position of string
            LastLength = FirstLength + sPattern.Length;
            FirstString = mString.Substring(0, FirstLength);
            startSeat = Convert.ToInt32(Regex.Replace(FirstString, @"[^\d]+", "")); // remove non numeric data
            LastString = mString.Substring(LastLength, mString.Length - LastLength);
            endSeat = Convert.ToInt32(Regex.Replace(LastString, @"[^\d]+", ""));
            if (startSeat > endSeat)
            {
                int swap = startSeat;
                startSeat = endSeat;
                endSeat = swap;
            }

            while (startSeat <= endSeat)
            {
                ResultBuffer.AddRow();                                // adding splitted rows into buffer
                ResultBuffer.EventName1 = Row.EventName;
                ResultBuffer.OpponentName1 = Row.OpponentName;

                ResultBuffer.EventDate1 = Row.EventDate;
                ResultBuffer.EventTime1 = Row.EventTime;
                ResultBuffer.SeatSection1 = Row.SeatSection;
                ResultBuffer.SeatRow1 = Row.SeatRow;
                ResultBuffer.Seats1 = startSeat.ToString();
                ResultBuffer.NbrOfSeats1 = Row.NbrOfSeats;

                startSeat = startSeat + 1;

            }
         
        }
        else
        {
            startSeat = Convert.ToInt32(Regex.Replace(mString, @"[^\d]+", ""));
            ResultBuffer.AddRow();
            ResultBuffer.EventName1 = Row.EventName;
            ResultBuffer.OpponentName1 = Row.OpponentName;

            ResultBuffer.EventDate1 = Row.EventDate;
            ResultBuffer.EventTime1 = Row.EventTime;
            ResultBuffer.SeatSection1 = Row.SeatSection;
            ResultBuffer.SeatRow1 = Row.SeatRow;
            ResultBuffer.Seats1 = startSeat.ToString();
            ResultBuffer.NbrOfSeats1 = Row.NbrOfSeats;

        }

    }

}


Splitted Result:




No comments:

Post a Comment