![]() |
Web Development »
ASP.NET »
General
Intermediate
License: The Code Project Open License (CPOL)
How to use global variable in Microsoft SSIS (DTS) packageBy hong_wei_li@yahoo.comHow to use global variable in Microsoft SSIS (DTS) package |
VB, SQL, .NET, WinXP, Win2003, ASP.NET, WebForms, SQL 2005, VS2005, Architect, DBA, Dev
|
||||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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 :
name = strSQL scope = Package ( this means global variable for this package ) Data Type = string value = select * from table1
' 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
then click OK
then click Mappings on the left pane and make sure all the fields are mapped right then click OK
This way you have successfully used the global variable in Microsoft SSIS ( DTS) package.
Please enjoy,
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 28 Apr 2006 Editor: Sean Ewington |
Copyright 2006 by hong_wei_li@yahoo.com Everything else Copyright © CodeProject, 1999-2009 Web21 | Advertise on the Code Project |