views:

536

answers:

1

I'm using SSIS to import data from Excel sheets into SQL Server 2005, the data contains some columns with data that should have the value of the previous non-empty cell in the same column, it looks like this:

2005 | 15 | Something1
     | 12 | Something2
     | 14 | Something3
2006 | 10 | Something4
     |  2 | Something5
     | 18 | Something6
     | 15 | Something7

It should fill-in 2005 and 2006, but all I could get is nulls. Can I put this field's value in a variable if it's not null, and use it to replace the null values that come after it?

+3  A: 

You'll have to use a Script Component in the Data Flow and go through each row, using a variable to hold the last known value.

Update: Here's the code. I did this in VB in SSIS 2008, so there may be some editing you have to do (for example, you may have to use Dts.Variables["User::LastYear"].Value.ToInt32() instead of Variables.LastYear--I can't remember off the top of my head):

Dim LastYear As Integer

Public Overrides Sub PreExecute()
    MyBase.PreExecute()
    LastYear = 0
End Sub

Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
    If Row.year_IsNull Then
        Row.year = LastYear
    Else
        LastYear = Row.year
    End If
End Sub

Hopefully, this helps!

Eric
Thank you, This looks like it will work.
BlueEyes
Great solution, I also used the script component for many other cool things "for every row"!
BlueEyes
Glad you like it, but don't go too crazy with a Script Component. Remember, it iterates through <b>every</b> row. That means that it's very, very inefficient. If you can do something in sets, do it in sets. Use this only for stuff you need to iterate through (like the current example).
Eric