Thursday, February 13, 2014

Passing variable values from a parent package to child package

Recently, I had to write a complex package (according to me) where I set the value of a variable in a package and I wanted the same value to be sent to the child package to keep my package running smoothly and efficiently. So here's what I did..

Example: Variable name: Brand

1- Dynamically set the value of the variable Brand in the parent package.

2- Declare a variable with the same name Brand in the child package

3- Go to configurations

4- Create a configuration using the "parent package configuration" type



5- Set up this configuration using the following steps:







Voila! You're done! I hope this helped :) 

Tuesday, November 12, 2013

Using dynamic excel file names with dynamic sheet names

Recently, I came across a problem when writing an SSIS package. I had done the dynamic names of SSIS excel files with the same meta data but using dynamic sheet names for the meta data came as a bit of a challenge to me. After a lot of googling, I simplified the steps to the following



-Declare two variables of String type
    • SheetName - To fetch names of sheet from the excel file
    • FileName - To fetch names of excel files.
- Create a Script task from which you want to fetch your sheet names 





-Write the following code in the script task

Public Sub Main()


Dim xlApp As Excel.Application = New Excel.ApplicationClass()

Dim xlWorkBook As Excel.Workbook

Dim xlWorkSheet As Excel.Worksheet



xlWorkBook = xlApp.Workbooks.Add(Dts.Variables("FileName").Value)

xlWorkSheet = CType(xlWorkBook.Sheets(1), Excel.Worksheet)

Dts.Variables("SheetName").Value = xlWorkSheet.Name


Dts.TaskResult = ScriptResults.Success

End Sub


-You can loop the Sheets if there are multiple sheets.


-Then, create another variable with the name of SQLcommand and give the following in it's expression:

"Select * from ["+ @[User::SheetName]+"$A1:CJ]"

Note: I used A1:CJ as my own range, so give the range you want the sheet to pick up.



-Give this variable in your excel source




-Now, to come to the point where all the excel files in the folder are traversed dynamically. To do this, create another variable called "SourceFile" of String data type
-Give this variable (usually configurable ) a path of the folder where your excel files reside.
-Then use a Foreachloop task with the following settings: 



Then, in this Foreachloop task, place your script task and the data flow task where the excel is getting transformed.