SSIS Package variable changes type at run time
I have a package that I have just added a new variable to. The new variable's scope is package-level, the type is string, and the initial value is empty.
Then I set the value of that variable at run-time with a script task.
When I executed the package, it gave me an error that I was trying to change the data type of a variable which is not allowed.
I ran the package in the debugger, and when I put a watch on the new variable, it reported the datatype as Int32. I look up and to the left, and in the Variables window, the type of that same variable is clearly "String".
Anybody ever had this happen? I googled, and apparently I'm the first ever as far as I can tell. I've tried giving the variable a default string value at design time, and still it changes the datatype of the variable to Int32 with a value
of 0 when it runs.
Anybody else got any ideas for me?
-Tab Alleman
July 25th, 2012 2:21pm
Can you show us the script task code you're using to set the value of the variable?Phil Brammer | http://www.ssistalk.com | Twitter: http://twitter.com/PhilBrammer
Free Windows Admin Tool Kit Click here and download it now
July 25th, 2012 2:48pm
Sure, code is below. Incidentally, I set breakpoints on the pre-execute event of the Script task, and the post-execute event of the task before it. Before the script task, the Variable is of type String. When it hits the
pre-execute of the Script task, it changes to Int32.
The variable that changes type is "Var_SheetName".
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Math
Imports System.Xml
Imports Microsoft.SqlServer.Dts.Runtime
Public Class ScriptMain
Public Sub Main()
Dim excelFile As String
Dim connectionString As String
Dim excelConnection As OleDbConnection
Dim tablesInFile As DataTable
Dim currentTable As String
excelFile = Dts.Variables("Var_Input_File").Value.ToString
connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & excelFile & ";Extended Properties=Excel 8.0"
excelConnection = New OleDbConnection(connectionString)
excelConnection.Open()
tablesInFile = excelConnection.GetSchema("Tables")
currentTable = tablesInFile.Rows(0).Item("TABLE_NAME").ToString()
Dts.Variables("Var_SheetName").Value = Convert.ToString(currentTable)
Dts.TaskResult = Dts.Results.Success
End Sub
End Class
-Tab Alleman
July 25th, 2012 3:15pm
Hi Tab,
For me it seems the issue lies with the data type of the currentTable variable. You specify it as a string but when you assign a value to it the data type is String() which doesn't look good at first glance.
Though that does not explain why it becomes Int32 especially during the PreExecute event.
What is the value that it should pick up? Can you please check if the currentTable has the expected value? Use a MsgBox e.g. before the line of error and/or comment out the row where you give value to Var_SheetName.
When you created the Script task, did you define what variables would be available inside?
Have you created a copy of the package and deleted everything else but the variable and the script task to make debugging more precise? If so it might be safe to give us a copy of it (maybe even copy-pasting the xml code is enough).
Free Windows Admin Tool Kit Click here and download it now
July 25th, 2012 9:48pm
Hi Martin,
For what it is worth at this point, I did put a breakpoint in the script task code and was able to check that currentTable variable had the expected value.
The script task was created with the expected variables defined to be available in the code.
However, I have fixed the problem. On a hunch I created another new string variable and used that in place of the one that was mysteriously changing its datatype. The new variable doesn't change types and works fine. I
am chalking this up to random bugginess in SSIS.
Thanks to all who responded, and hopefully this helps somebody else someday.-Tab Alleman
July 26th, 2012 3:30pm