CFMX and SQL Server Data Transformation Services (Part two)

CFMX and SQL Server Data Transformation Services (Part Two of Two)
Author: Tedd Van Diest

*Use SQL Query Analyzer to run SQL Scripts in the following order.

*Note: sp_displayoaerrorinfo.sql, spDisplayPKGErrors.sql, spExecuteDTS.sql
credit goes to Pengoworks.com
SQL Server
sp_displayoaerrorinfo.sql [1]
CREATE PROC sp_displayoaerrorinfo
@object as int
AS

DECLARE @hr int
DECLARE @output varchar(255)
DECLARE @source varchar(255)
DECLARE @description varchar(255)

PRINT 'OLE Automation Error Information'

EXEC @hr = sp_OAGetErrorInfo @object, @source OUT, @description OUT
IF @hr = 0
BEGIN
SELECT @output = ' Source: ' + @source
PRINT @output
SELECT @output = ' Description: ' + @description
PRINT @output
END
ELSE
BEGIN
PRINT ' sp_OAGetErrorInfo failed.'
RETURN
END

GO

spDisplayPKGErrors.sql [2]
-- display errors from spExecuteDTS execution
CREATE PROC spDisplayPKGErrors
@oPkg As integer
AS

SET NOCOUNT ON

DECLARE @StepCount int
DECLARE @Steps int
DECLARE @Step int
DECLARE @StepResult int
DECLARE @oPkgResult int
DECLARE @hr int

DECLARE @StepName varchar(255)
DECLARE @StepDescription varchar(255)

IF OBJECT_ID('tempdb..#PkgResult') IS NOT NULL
DROP TABLE #PkgResult

CREATE TABLE #PkgResult
(
StepName varchar(255) NOT NULL,
StepDescription varchar(255) NOT NULL,
Result bit NOT NULL
)

SELECT @oPkgResult = 0

EXEC @hr = sp_OAGetProperty @oPkg, 'Steps', @Steps OUTPUT
IF @hr <> 0
BEGIN
PRINT '*** Unable to get steps'
EXEC sp_displayoaerrorinfo @oPkg , @hr
RETURN 1
END

EXEC @hr = sp_OAGetProperty @Steps, 'Count', @StepCount OUTPUT
IF @hr <> 0
BEGIN
PRINT '*** Unable to get number of steps'
EXEC sp_displayoaerrorinfo @Steps , @hr
RETURN 1
END

WHILE @StepCount > 0
BEGIN
EXEC @hr = sp_OAGetProperty @Steps, 'Item', @Step OUTPUT, @StepCount
IF @hr <> 0
BEGIN
PRINT '*** Unable to get step'
EXEC sp_displayoaerrorinfo @Steps , @hr
RETURN 1
END

EXEC @hr = sp_OAGetProperty @Step, 'ExecutionResult', @StepResult OUTPUT
IF @hr <> 0
BEGIN
PRINT '*** Unable to get ExecutionResult'
EXEC sp_displayoaerrorinfo @Step , @hr
RETURN 1
END


EXEC @hr = sp_OAGetProperty @Step, 'Name', @StepName OUTPUT
IF @hr <> 0
BEGIN
PRINT '*** Unable to get step Name'
EXEC sp_displayoaerrorinfo @Step , @hr
RETURN 1
END

EXEC @hr = sp_OAGetProperty @Step, 'Description', @StepDescription OUTPUT
IF @hr <> 0
BEGIN
PRINT '*** Unable to get step Description'
EXEC sp_displayoaerrorinfo @Step , @hr
RETURN 1
END

INSERT #PkgResult VALUES(@StepName, @StepDescription, @StepResult)
PRINT 'Step ' + @StepName + ' (' + @StepDescription + ') ' + CASE WHEN @StepResult = 0 THEN 'Succeeded' ELSE 'Failed' END

SELECT @StepCount = @StepCount - 1
SELECT @oPkgResult = @oPkgResult + @StepResult
END

SELECT * FROM #PkgResult

IF @oPkgResult > 0
BEGIN
PRINT 'Package had ' + CAST(@oPkgResult as varchar) + ' failed step(s)'
RETURN 9
END
ELSE
BEGIN
PRINT 'Packge Succeeded'
RETURN 0
END

GO

spExecuteDTS.sql [3]
CREATE PROC spExecuteDTS
@Server varchar(255),
@PkgName varchar(255), -- Package Name (Defaults to most recent version)
@ServerPWD varchar(255) = Null, -- Server Password if using SQL Security to load Package (UID is SUSER_NAME())
@IntSecurity bit = 0, -- 0 = SQL Server Security, 1 = Integrated Security
@PkgPWD varchar(255) = '' -- Package Password
AS

SET NOCOUNT ON
/*
Return Values
- 0 Successfull execution of Package
- 1 OLE Error
- 9 Failure of Package
*/
DECLARE @hr int, @ret int, @oPKG int, @Cmd varchar(1000)

-- Create a Pkg Object
EXEC @hr = sp_OACreate 'DTS.Package', @oPKG OUTPUT
IF @hr <> 0
BEGIN
PRINT '*** Create Package object failed'
EXEC sp_displayoaerrorinfo @oPKG, @hr
RETURN 1
END

-- Evaluate Security and Build LoadFromSQLServer Statement
IF @IntSecurity = 0
SET @Cmd = 'LoadFromSQLServer("' + @Server +'", "' + SUSER_SNAME() + '", "' + @ServerPWD + '", 0, "' + @PkgPWD + '", , , "' + @PkgName + '")'
ELSE
SET @Cmd = 'LoadFromSQLServer("' + @Server +'", "", "", 256, "' + @PkgPWD + '", , , "' + @PkgName + '")'

EXEC @hr = sp_OAMethod @oPKG, @Cmd, NULL

IF @hr <> 0
BEGIN
PRINT '*** LoadFromSQLServer failed'
EXEC sp_displayoaerrorinfo @oPKG , @hr
RETURN 1
END

-- Execute Pkg
EXEC @hr = sp_OAMethod @oPKG, 'Execute'
IF @hr <> 0
BEGIN
PRINT '*** Execute failed'
EXEC sp_displayoaerrorinfo @oPKG , @hr
RETURN 1
END

-- Check Pkg Errors
EXEC @ret=spDisplayPkgErrors @oPKG

-- Unitialize the Pkg
EXEC @hr = sp_OAMethod @oPKG, 'UnInitialize'
IF @hr <> 0
BEGIN
PRINT '*** UnInitialize failed'
EXEC sp_displayoaerrorinfo @oPKG , @hr
RETURN 1
END

-- Clean Up
EXEC @hr = sp_OADestroy @oPKG
IF @hr <> 0
BEGIN
EXEC sp_displayoaerrorinfo @oPKG , @hr
RETURN 1
END

RETURN @ret

GO

Tracking.sql [4]
CREATE TABLE Tracking(
browser varchar(50) NULL,
[date] datetime NULL
);
spGetStats.sql [5]
CREATE PROC spGetStats

AS


CREATE TABLE #tempTracking(
[date] datetime,
visitorCount int,
browser varchar(50)
)

INSERT INTO #tempTracking
SELECT [date],COUNT(browser) as visitorCount,browser
FROM tracking
group by [date],browser

SELECT SUM(visitorCount) as visitorSum, browser
FROM #tempTracking
GROUP BY browser
ORDER BY browser

DROP TABLE #tempTracking

GO

SQL Server DTS Package
Step 1:
Open SQL Server Enterprise Manager, Microsoft SQL Servers > SQL Server Group > (Server name) Server > Data Transformation Services then right-click "Local Packages" and select "New Package". When your design sheet window pops up, select "Package" menu item and the select "Save As", then type "cfmxDTS" in the Package Name field and click OK.
Step 2:
With your package design sheet still open, click the Text File (Source) icon under your connection objects and in the connection properties window select "New Connection" then type "Tracking datasource", select " Text File (Source)" if it is not already selected then under file name browse to your "browserLog.txt" file and click OK.In the text file properties window select Delimited, select "ANSI" for file type, "0" for skip rows, " {CR}{LF}" for row delimiter, "<none>" for text qualifier then click next, select Comma as the delimiter then Finish > OK.
Step 3:
Again with your package design sheet still open, click the Microsoft OLE DB Provider for SQL Server icon under your connection objects, select New Connection and type "Tracking Destination" in the New Connection field, select "Tracking Destination" as the datasource, select (local) for the server, select Use Windows Authentication and select "cfmxDTSDB" for the database, click OK.

Your design sheet should now look like this...
Step 4:
Again with your package design sheet still open, again... Click the Transform Data Task icon under your Task objects, click your "Tracking datasource" object on the sheet, then click on your "Tracking Destination" object on the sheet.

Your design sheet should now look like this.
Make sure your arrow is pointing from your source to your destination..


Next, double click the transform data task (not the icon, the line with the arrow) and type "[1-1] Transform tracking data" in the description field, click the Destination tab and make sure you select the "Tracking" table from the table name select list, click OK. Now we are ready to test it...


Go to http://localhost/cfmxDTS and click the submit button to populate the text file with 1000 records, or click it 5 or 6 or 10 times just to see how slow it actualy appends data to the file. Next click the "DTS Package" link on the left hand navigation and click the submit button to run the DTS Package and then you will see the benefits of using a tool like DTS to move the data from one source to another.

So that's all there is to it... If you have any questions feel free to contact me here.. Just click on the image and a contact form will pop up.. And yes I do look like that.

Until next time... (Probably Adjacency Lists)

Enjoy,
Tedd
All ColdFusion Tutorials By Author: Tedd Van Diest
Download the EasyCFM.COM Browser Toolbar!