Microsoft SQL Server 2005 Integration Services has new interface about DTS ( data transformation service). It has no many helpful examples for developers to follow. One of sticky things is to how to use global variables. Hopefully the following will save you a lot of frustrations.
the scenario is like this: (1). I want to dump the data from one table (table1, primary key is theId - int, fields are FirstName - varchar(50), LastName - varchar(50)) in database 1 (db1) to another same structure table(table2, primary key is theId - int, fields are FirstName - varchar(50), LastName - varchar(50)) in database 2(db2) based on table1.theId > max(table2.theId). This is a very common scenario to transfer the data. (2). I need to pass table2.theId to the SQL statement: SELECT * FROM table1 WHERE table1.theId > max(table2.theId) (3) you make sure table1 has more rows and TheId is bigger than table2
Instead of thinking of table2.theId as a variable to pass, we can pass all the SQL statement as a global string variable.
Here are steps how to achieve this :
- start Microsoft SQL Server 2005 -> SQL Server Business Intelligence Development Studio
- click File -> New -> Project -> choose "Integration Services Project" -> name your project name like ssisTest
- click SSIS -> Variable
- Under Variable window, Add variable:
name = strSQL
scope = Package ( this means global variable for this package )
Data Type = string
value = select * from table1
- At the left bottom corner, you will connection manager, put your cursor at the area, right mouse click to add New OLE DB connection. You add one connection for db1 and another connection for db2.
- click control Flow tab, ctrl+alt+x to get toolbox, drag a Script Task under Control Flow Items to control Flow tab. Highlight Script Task 1 and double click Script Task 1 icon -> Highlight "Script" on the left panel of Script Task Editor -> input "strSQL" for ReadWriteVariables -> click "Design Script..."
- Here are the code
Public Class ScriptMain
Public Sub Main()
Dim strConnection As String
strConnection = "Data Source=server2;Initial Catalog=db2;Integrated Security=True"
Dim connection As New SqlConnection(strConnection)
Dim strQuery As String
strQuery = "select max(TheId) MaxTheId from Table2"
Dim command As New SqlCommand(strQuery, connection)
Dim srReader As SqlDataReader
srReader = command.ExecuteReader()
Dim strTheId As String
strTheId = srReader.Item(0).ToString
Dts.Variables("strSQL").Value = "SELECT * FROM TABLE1 WHERE TheId > " & strTheId
Dts.TaskResult = Dts.Results.Success
Then save it and close this window
- click Data Flow tab and click the link on the center of this tab to add a data flow task
- On Data Flow tab, drag "OLE DB source" from toolbox's "Data Flow Source" section double click this OLE DB Source icon and under OLE DB Source editor,
- choose server1.db1 for OLE DB connection manager
- Data access mode: SQL command from variable
- Variable name: User::strSQL
then click OK
- On Data Flow tab, drag "OLE DB Destination" from toolbox's "Data Flow Destinations" section double click this OLE DB Destination icon and under OLE DB Destination editor,
- choose server2.db2 for OLE DB connection manager
- Data access mode: Table or view - fast load
- Name of the table or the view: [dbo].[table2]
then click Mappings on the left pane and make sure all the fields are mapped right then click OK
- Now you can click Build -> build ssisTest and click Debug -> Start with debugging
- You can verify the data which are inserted to db2's table2 from db1's table1
This way you have successfully used the global variable in Microsoft SSIS ( DTS) package.