SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Script Task In SSIS to Check column with Same value and change it accordingly


Script Task In SSIS to Check column with Same value and change it accordingly

Author
Message
rb_patel
rb_patel
SSC Rookie
SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)

Group: General Forum Members
Points: 31 Visits: 125
Hi All

I wanted to ask, If using SSIS Script Task Following Task can be achieved.

Example

Name Value
Ab1 23
Ab1 25
Ab1 26

Desired Output as below

Name Value
Ab1^1 23
Ab1^2 25
Ab1^3 26

So simply check the incoming column name and if it appears for 1st time add ^1, if the same appears for 2nd time and 3rd Add ^2 and ^3 respectively behind the Name column.


And the input coming from top is order by Name

thanks
R
John Rowan
John Rowan
SSCertifiable
SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)

Group: General Forum Members
Points: 6128 Visits: 4551
The short answer is 'Yes' a script task can do this. You would first ensure that the data is ordered properly, declare a package variable as the counter, a package variable for the Name value, and write the script to review the increment the value of the counter variable when the Name value = the variable Name value.

What will you be doing with the data once it is in the format Name^#?

John Rowan

======================================================
======================================================
Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
rb_patel
rb_patel
SSC Rookie
SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)

Group: General Forum Members
Points: 31 Visits: 125
Thanks John

For such a quick and to the point reply

I made following code

Declared following variable as DIM in Public Class
1.variable as the counter = Counter
2.package variable for the Name value = Name


If (Counter = Name) Then
Row.Name = row.Name + Counter
End If
Row.Name.ToString()
End Sub

But Not achieving the Result.
John Rowan
John Rowan
SSCertifiable
SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)

Group: General Forum Members
Points: 6128 Visits: 4551
OK, I mocked up an example based off of this table:


IF OBJECT_ID('dbo.SSISTest') IS NOT NULL
DROP TABLE dbo.SSISTest
GO
CREATE TABLE dbo.SSISTest(
Name varchar(10) NULL,
Value int NULL
)
GO
INSERT INTO dbo.SSISTest
SELECT 'Ab1',23 UNION ALL
SELECT 'Ab1',25 UNION ALL
SELECT 'Ab1',26 UNION ALL
SELECT 'Ab2',23 UNION ALL
SELECT 'Ab2',25 UNION ALL
SELECT 'Ab3',26



Create 2 variables:
--USER::Name as string
--USER::Counter as int32

Then, I created a data flow. The first task inside the data flow is an OLE DB Source pointed to the dbo.SSISTest table. The next task is a Script Transformation with the following code:


' Microsoft SQL Server Integration Services user script component
' This is your new script component in Microsoft Visual Basic .NET
' ScriptMain is the entrypoint class for script components
Option Strict Off
Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Pipeline.Wrapper
Imports Microsoft.SqlServer.Dts.Runtime.Wrapper

Public Class ScriptMain
Inherits UserComponent

Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
'
Dim Counter As Integer
Dim Name As String
Dim vars As IDTSVariables90

Me.VariableDispenser.LockOneForRead("User::Name", vars)
Name = vars(0).Value
vars.Unlock()

Me.VariableDispenser.LockOneForRead("User::Counter", vars)
Counter = vars(0).Value
vars.Unlock()

If Name = Row.Name Then
Counter = Counter + 1
Else
Counter = 1
End If

' Write values back to variables
Me.VariableDispenser.LockOneForWrite("User::Name", vars)
vars(0).Value = Row.Name
vars.Unlock()

Me.VariableDispenser.LockOneForWrite("User::Counter", vars)
vars(0).Value = Counter
vars.Unlock()

' Update row in pipeline
Row.Name = Row.Name + "^" + Counter.ToString
End Sub
End Class



I routed the script component to an OLE DB Destination linked to this table:

IF OBJECT_ID('dbo.SSIS_Dest') IS NOT NULL
DROP TABLE dbo.SSIS_Dest
GO
CREATE TABLE dbo.SSIS_Dest(
Name varchar(10) NULL,
Value int NULL
)
GO



This is the results it produced for me:

Name Value
Ab1^1 23
Ab1^2 25
Ab1^3 26
Ab2^1 23
Ab2^2 25
Ab3^1 26

John Rowan

======================================================
======================================================
Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search