Advanced users familiar with the Data Transformation Services (DTS) object model can build their own transformable subscription DTS packages in Microsoft® Visual Basic™. For information about programming to the DTS object model, see DTS Programming Reference.
A transformable subscription is a subscription in which the data is modified as it flows from Publisher to Subscriber. In replication programming, a Visual Basic program for a transformable subscription closely resembles that of a DTS Visual Basic program used outside of replication. This sample is located in C:\Program Files\Microsoft SQL Server\80\Tools\DevTools\Samples\sqlrepl\repldts.
The program in the repldts folder is a sample of how to write a transformable subscription using Visual Basic.
To run the sample program
goPackage.SavetoSQLServer "MyServerName", "sa", ""
This section describes key parts of the sample Visual Basic program.
Dim oConnection As DTS.Connection
Set Connection = goPackage.Connections.New("SQLReplication.OLEDB")
oConnection.Name = "Publisher article 'Employees'"
oConnection.ID = 1
oConnection.ConnectImmediate = False
oConnection.ConnectionProperties("Column List") = _
"[EmployeeID],[LastName],[FirstName],[Title],
[BirthDate],[HireDate],[Address],[City],[Region],
[PostalCode],[Country],[HomePhone],[Extension]
[ReportsTo]"
goPackage.Connections.Add oConnection
Set oConnection = Nothing
Use additional Execute SQL custom tasks to define SQL scripts to be applied after the data has been copied to the destination (for example, a script containing index generation statements (not shown)).
Use the following conventions when naming tasks in a replication DTS program (required by the replication agents):
In the following code sample, the Execute SQL task name "Employees_pre_ignore_error" (line 5) means that the article name is Employees, the task occurs before the snapshot data is copied, and that program execution should continue if a script error is encountered.
Dim oTask As DTS.Task
Dim oCustomTask0 As DTS.ExecuteSQLTask
Set oTask = goPackage.Tasks.New("DTSExecuteSQLTask")
Set oCustomTask0 = oTask.CustomTask
oCustomTask0.Name = "Employees_pre_ignore_error"
oCustomTask0.Description = "Pre script for article employees"
oCustomTask0.SQLStatement = _
"If object_id('MyEmployees') is NOT NULL _
BEGIN Drop Table MyEmployees END _
Create Table MyEmployees _
([EmployeeID] [int] NOT NULL,
[LastName] [nvarchar] (20) NOT NULL,
[FirstName] [nvarchar] (10) NOT NULL,
[Title] [nvarchar] (30) NULL,
[Birthdate] [datetime] NULL,
[HireDate] [datetime] NULL,
[Address] [nvarchar] (255) NULL,
[HomePhone] [nvarchar] (24) NULL,
[Extension] [nvarchar] (4) NULL,
[ReportsTo] [int] NULL,
CONSTRAINT [PK_Employees] PRIMARY KEY _
CLUSTERED([EmployeeID]))"
CustomTask0.ConnectionID = 2
goPackage.Tasks.Add oTask
Set CustomTask0 = Nothing
Set oTask = Nothing
Dim oTransformation As DTS.Transformation
Dim oTransProps As DTS.Properties
Dim oColumn As DTS.Column
Dim oCustomTask1 As DTS.DataDrivenQueryTask
Set oTask = goPackage.Tasks.New("DTSDataDrivenQueryTask")
Set oCustomTask1 = oTask.CustomTask
oCustomTask1.Name = "Employees"
oCustomTask1.Description = "Transformations for article Employees"
oCustomTask1.SourceConnectionID = 1
oCustomTask1.SourceObjectName = "nothing" 'Experiment
oCustomTask1.DestinationConnectionID = 2
oCustomTask1.DestinationObjectName = "MyEmployees"
oCustomTask1.InsertQuery = "INSERT INTO _
MyEmployees values (?,?,?,?,?,?,?,?,?,?)"
oCustomTask1.UpdateQuery = "UPDATE _MyEmployees _
SET LastName=?, FirstName=?, Title=?, Birthdate=?, _
HireDate=?, Address=?, HomePhone=?, Extension=?, _
ReportsTo=? where EmployeeID=?"
oCustomTask1.DeleteQuery = "DELETE MyEmployees _
WHERE EmployeeID = ?"
Set oColumn = oTransformation.DestinationColumns.New("HireDate", 6)
oColumn.Name = "HireDate"
oColumn.Ordinal = 6
oTransformation.DestinationColumns.Add oColumn
Set oColumn = Nothing
...
Set oColumn = oTransformation.DestinationColumns.New("Address", 7)
oColumn.Name = "Address"
oColumn.Ordinal = 7
oTransformation.DestinationColumns.Add oColumn
Set oColumn = Nothing
...
Set oColumn = oCustomTask1.InsertQueryColumns.New("EmployeeID", 1)
oColumn.Name = "EmployeeID"
oColumn.Ordinal = 1
oCustomTask1.InsertQueryColumns.Add oColumn
Set oColumn = Nothing
Set oColumn = oCustomTask1.InsertQueryColumns.New("LastName", 2)
oColumn.Name = "LastName"
oColumn.Ordinal = 2
oCustomTask1.InsertQueryColumns.Add oColumn
Set oColumn = Nothing
...
Similar code is used for the UPDATE and DELETE queries. When defining the columns for those queries, remember that the order of the columns must follow the order of the parameters specified by the question mark (?) characters in the InsertQuery and DeleteQuery definitions. For example, in this sample, the DELETE query uses only the EmployeeId column; therefore, only code for that column is used.
Set oColumn = oCustomTask1.DeleteQueryColumns.New("EmployeeID", 1)
oColumn.Name = "EmployeeID"
oColumn.Ordinal = 1
oCustomTask1.DeleteQueryColumns.Add oColumn
Set oColumn = Nothing
Set oTransProps = oTransformation.TransformServerProperties
...
oTransProps("Text") = oTransProps("Text") &
" DTSDestination(""Address"") = DTSSource(""Address"") _
& "","" & DTSSource(""City"") &"",""& DTSSource(""Region"") _
&"",""& DTSSource(""PostalCode"")" & vbCrLf
...
GoPackage.SaveToSQLServer "myServerName", "sa", ""
...
oTransformation.TransformFlags = 63