Data Transformation Services (DTS) were added to SQL Server 7 and allow us to combine several data-related tasks into one common object. In this article Tim shows us how to create a DTS package with SQL Server 2000 that will access a database and email the results of a query to some sales executives. He also shows us how to execute and error trap DTS packages from within an ASP script.
Creating DTS Packages With SQL Server 2000 - Executing our DTS package in ASP (Page 5 of 6 )
Thanks to the way everything in Windows is tightly integrated, we only need a couple of lines to execute our DTS package. We need to instantiate a new DTS package object. The DTS package object is a COM object with the ProgID of "DTS.Package". Once we have instantiated a new DTS package object, we can pass-in our global variable (for the WHERE clause), and execute each step in our DTS package sequentially.
Create a new ASP script called "testdtspkg.asp" and enter the following code into it:
<title>Sales Report DTS Package</title>
const DTSSQLStgFlag_Default = 0
const DTSStepExecResult_Failure = 1
set objDTSPackage = Server.CreateObject("DTS.Package")
As you can see in our example above, several of the parameters are optional. Once we have loaded our package, we have to assign a value to its global variable, gPaymentTerm. Remember that this global variable will replace the "?" in the WHERE clause of our query to the sales and stores tables of the pubs database? We use the GlobalVariables collection to do this:
Next, we use the Steps collection of our DTS package object to execute each step in our DTS package sequentially. First step is the OLEDB connection, then the execute SQL task, and lastly the ActiveX script task. For each step, we check whether it succeeded/failed using its ExecutionResult variable and note this in the strResult string variable. If a step fails, then the blnSucceeded variable is set to false.
Lastly, we output whether or not our DTS package succeeded/failed, as well as the details of each step that it contains. When I ran the script in my browser, it gave me the following results:
Notice that only the tasks are listed here, and not the actual connection object. In our example, the sales email was sent to email@example.com and firstname.lastname@example.org. Here's a snippet from the email attachment (c:\salesreport.txt):
Store #7066 sold 50 items with payment type Net 30
Store #7067 sold 40 items with payment type Net 30
Store #7067 sold 20 items with payment type Net 30