To configure this Script Component example
- Create and save a text file named parentchilddata.txt that contains the following source data:
********** PARENT 1 DATA child 1 data child 2 data child 3 data child 4 data ********** PARENT 2 DATA child 5 data child 6 data child 7 data child 8 data **********
- Open SQL Server Management Studio and connect to an instance of SQL Server.
- Select
a destination database, and open a new query window. In the query
window, execute the following script to create the destination tables:
CREATE TABLE [dbo].[Parents]( [ParentID] [int] NOT NULL, [ParentRecord] [varchar](32) NOT NULL, CONSTRAINT [PK_Parents] PRIMARY KEY CLUSTERED ([ParentID] ASC) ) GO CREATE TABLE [dbo].[Children]( [ChildID] [int] NOT NULL, [ParentID] [int] NOT NULL, [ChildRecord] [varchar](32) NOT NULL, CONSTRAINT [PK_Children] PRIMARY KEY CLUSTERED ([ChildID] ASC) ) GO ALTER TABLE [dbo].[Children] ADD CONSTRAINT [FK_Children_Parents] FOREIGN KEY([ParentID]) REFERENCES [dbo].[Parents] ([ParentID])
- Open Business Intelligence Development Studio and create a new Integration Services package named SplitParentChild.dtsx.
- Add a Flat File connection manager to the package, name it ParentChildData, and configure it to connect to the parentchilddata.txt file that you created in a previous step.
- Add an OLE DB connection manager to the package and configure it to connect to the instance of SQL Server and the database in which you created the destination tables.
- Add a Data Flow task to the package and click the Data Flow tab of SSIS Designer.
- Add a Flat File Source to the data flow and configure it to use the ParentChildData connection manager. On the Columns page of the Flat File Source Editor, select the single available external column.
- Add a Script Component to the data flow and configure it as a transformation. Connect the output of the Flat File Source to the Script Component.
- Double-click the Script component to display the Script Transformation Editor.
- On the Input Columns page of the Script Transformation Editor, select the single available input column.
- On the Inputs and Outputs page of the Script Transformation Editor, select Output 0, rename it to ParentRecords, and set its SynchronousInputID to None. Create 2 output columns:
- ParentID (the primary key), of type four-byte signed integer [DT_I4]
- ParentRecord, of type string [DT_STR] with a length of 32.
- Create a second output and name it ChildRecords. The SynchronousInputID of the new output is already set to None. Create 3 output columns:
- ChildID (the primary key), of type four-byte signed integer [DT_I4]
- ParentID (the foreign key), also of type four-byte signed integer [DT_I4]
- ChildRecord, of type string [DT_STR] with a length of 50
- On the Script page of the Script Transformation Editor, click Edit Script. In the ScriptMain class, enter the code shown in the example. Close the script development environment and the Script Transformation Editor.
- Add a SQL Server Destination to the data flow. Connect the ParentRecords output of the Script Component to this destination.Configure it to use the OLE DB connection manager and the Parents table.
- Add another SQL Server Destination to the data flow. Connect the ChildRecords output of the Script Component to this destination. Configure it to use the OLE DB connection manager and the Children table.
- Run the package. After the package has finished, examine the parent and child records in the two SQL Server destination tables.
public override void Input0_ProcessInputRow(Input0Buffer Row) { int static_Input0_ProcessInputRow_childCounter = 0; int static_Input0_ProcessInputRow_parentCounter = 0; bool static_Input0_ProcessInputRow_nextRowIsParent = false; // If current row starts with separator characters, // then following row contains new parent record. if (Row.Column0.StartsWith("***")) { static_Input0_ProcessInputRow_nextRowIsParent = true; } else { if (static_Input0_ProcessInputRow_nextRowIsParent) { // Current row contains parent record. static_Input0_ProcessInputRow_parentCounter += 1; this.ParentRecordsBuffer.AddRow(); this.ParentRecordsBuffer.ParentID = static_Input0_ProcessInputRow_parentCounter; this.ParentRecordsBuffer.ParentRecord = Row.Column0; static_Input0_ProcessInputRow_nextRowIsParent = false; } else { // Current row contains child record. static_Input0_ProcessInputRow_childCounter += 1; this.ChildRecordsBuffer.AddRow(); this.ChildRecordsBuffer.ChildID = static_Input0_ProcessInputRow_childCounter; this.ChildRecordsBuffer.ParentID = static_Input0_ProcessInputRow_parentCounter; this.ChildRecordsBuffer.ChildRecord = Row.Column0; } } }
Taken the reference from the site -
http://msdn.microsoft.com/en-us/library/ms345160%28v=sql.105%29.aspx
No comments:
Post a Comment