Click here to Skip to main content
6,295,667 members and growing! (11,165 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate

How to use global variable in Microsoft SSIS (DTS) package

By hong_wei_li@yahoo.com

How to use global variable in Microsoft SSIS (DTS) package
VB, SQL, .NET, WinXP, Win2003, ASP.NET, WebForms, SQL 2005, VS2005, Architect, DBA, Dev
Posted:28 Apr 2006
Views:62,822
Bookmarked:13 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
6 votes for this article.
Popularity: 1.56 Rating: 2.00 out of 5
2 votes, 33.3%
1
2 votes, 33.3%
2
1 vote, 16.7%
3
1 vote, 16.7%
4

5

Introduction

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 :

1. start Microsoft SQL Server 2005 -> SQL Server Business Intelligence Development Studio

2. click File -> New -> Project -> choose "Integration Services Project" -> name your project name like ssisTest

3. click SSIS -> Variable

4. Under Variable window, Add variable:
name = strSQL
scope = Package ( this means global variable for this package )
Data Type = string
value = select * from table1

5. 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.

6. 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..."

7. Here are the code
' Microsoft SQL Server Integration Services Script Task
' Write scripts using Microsoft Visual Basic
' The ScriptMain class is the entry point of the Script Task.

Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Imports System.Data.SqlClient
Imports System.Data.Common

Public Class ScriptMain

' The execution engine calls this method when the task executes.
' To access the object model, use the Dts object. Connections, variables, events,
' and logging features are available as static members of the Dts class.
' Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
'
' To open Code and Text Editor Help, press F1.
' To open Object Browser, press Ctrl+Alt+J.

Public Sub Main()
'
' Add your code here
'
Dim strConnection As String
strConnection = "Data Source=server2;Initial Catalog=db2;Integrated Security=True"
Dim connection As New SqlConnection(strConnection)
connection.Open()
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 = "5"

While (srReader.Read())
strTheId = srReader.Item(0).ToString
End While

connection.Close()

' here create the right strSQL string
Dts.Variables("strSQL").Value = "SELECT * FROM TABLE1 WHERE TheId > " & strTheId

' this is to test if the strSQL string is right
MsgBox(Dts.Variables("strSQL").Value.ToString)

Dts.TaskResult = Dts.Results.Success
End Sub

End Class

Then save it and close this window

8. click Data Flow tab and click the link on the center of this tab to add a data flow task
9. 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,
a. choose server1.db1 for OLE DB connection manager
b. Data access mode: SQL command from variable
c. Variable name: User::strSQL
then click OK

10. 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,
a. choose server2.db2 for OLE DB connection manager
b. Data access mode: Table or view - fast load
c. 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

11. Now you can click Build -> build ssisTest and click Debug -> Start with debugging

12. 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.

Please enjoy,

- Hongwei Li


if you have any comments, please email me at hong_wei_li@yahoo.com

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

hong_wei_li@yahoo.com


Member
.Focus on database (SQL Server 2005/2000/7/6.5 and Oracle 10g) development with C#,

ASP.NET, ASP, Java, PHP.
.Like to work with MS Server 2005 SSIS and report service
.Like full cycle software design, development and deployment
.Microsoft Certified SQL Server Developer and DBA.
Occupation: Web Developer
Location: United States United States

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 6 of 6 (Total in Forum: 6) (Refresh)FirstPrevNext
GeneralSource Files Not Found PinmemberJavad Bayani23:18 6 Jul '07  
GeneralUse ExecSQL task instead of script Pinmemberr.stropek21:23 4 Jun '06  
GeneralHow to use SQL Command variable in OLEDB Destination? Pinmemberjchouy13:06 19 May '06  
GeneralRe: How to use SQL Command variable in OLEDB Destination? Pinmemberr.stropek21:32 4 Jun '06  
GeneralRe: How to use SQL Command variable in OLEDB Destination? Pinmemberjchouy9:18 8 Jun '06  
GeneralRe: How to use SQL Command variable in OLEDB Destination? Pinmemberr.stropek23:19 8 Jun '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 28 Apr 2006
Editor:
Copyright 2006 by hong_wei_li@yahoo.com
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project