Database Journal
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Links Database Forum

» Database Journal Home
» Database Articles
» Database Tutorials
MS SQL
Oracle
DB2
MS Access
MySQL
» RESOURCES
Database Tools
SQL Scripts & Samples
Links
» Database Forum
» Sitemap
Free Newsletters:
DatabaseDaily  
News Via RSS Feed


follow us on Twitter
Database Journal |DBA Support |SQLCourse |SQLCourse2
 

Featured Database Articles

MS SQL

Posted Sep 24, 2000

Data Transformation Services (DTS) - Page 2

By Darren Green

Advanced

Here we cover some of the more advanced uses you can make of the OLE Automation stored procedures and DTS.

Package Properties and GlobalVariables

The most obvious advantage of using the OLE Automation stored procedures is the ability to dynamically alter the package prior to execution. For example you can alter a connection's DataSource property, which is the filename property for a Text file Connection. I prefer a variation on this theme whereby you set global variables not the actual property, then use an ActiveX Script Task or a Dynamic Properties Task (SQL 2000 Only) within the package to amend the appropriate property. This way if you change your package, you don't have to change your SQL code. In addition it makes it easer to change the execution method, for example you might like to take advantage of the new functionality in the SQL 2000 dtsrun and pass in global variables, or you might decide to utilise the package in a program. In each case you can write the package once, and with minimal effort use different code to set your global variables and execute the package.

For clarity variable declaration has been removed from the code snippets below

Set a Global Variable:

EXEC @hr = sp_OASetProperty @oPKG, 'GlobalVariables("MyGVName").Value', 'MyGVValue'
IF @hr <> 0
BEGIN
    PRINT '***  GlobalVariable Assignment Failed'
	EXEC sp_displayoaerrorinfo @oPKG, @hr
	RETURN
END

 

As well as setting global variables, you can read them after execution, which is one method of returning data from a DTS package.

Return a single Global Variable:

EXEC @hr = sp_OAGetProperty @oPKG, 'GlobalVariables("MyGVName").Value', @GVOutput OUT
IF @hr <> 0
BEGIN
    PRINT '***  GlobalVariable Read Failed'
	EXEC sp_displayoaerrorinfo @oPKG, @hr
	RETURN
END
PRINT 'My Global Variable is - ' + @GVOutput

Enumerate all global variables:

-- Get Global Variables Count
EXEC @hr = sp_OAGetProperty @oPKG, 'GlobalVariables.Count', @Count OUT
IF @hr <> 0
BEGIN
    PRINT '***  Get GlobalVariable Count Failed'
    EXEC sp_displayoaerrorinfo @oPKG, @hr
    RETURN
END
PRINT 'GlobalVariables.Count: ' + CAST(@Count as varchar(4))

-- List Global Variable Names & Values
WHILE @i <= @Count
BEGIN
    PRINT 'Global Variable Index: ' + CAST(@i as varchar(4))
    -- Get Global Variable Name
    SET @cmd = 'GlobalVariables.Item(' + CAST(@i as varchar(4)) + ').Name'
    EXEC @hr = sp_OAGetProperty @oPKG, @cmd, @property OUT
    IF @hr <> 0
    BEGIN
        PRINT '*** Get Global Variable Name Failed'
        EXEC sp_displayoaerrorinfo @oPKG, @hr
        RETURN
    END
    PRINT '  GlobalVariable Name: ' + @property
    -- Get Global Variable Value
    SET @cmd = 'GlobalVariables.Item(' + CAST(@i as varchar(4)) + ').Value'
    EXEC @hr = sp_OAGetProperty @oPKG, @cmd, @property OUT
    IF @hr <> 0
    BEGIN
        PRINT '*** Get Global Variable Value Failed'
        EXEC sp_displayoaerrorinfo @oPKG, @hr
        RETURN
    END
    PRINT '  GlobalVariable Value: ' + @property
    SET @i = @i + 1
END

 

Package Execution Errors

The other big issue with executing packages is to ensure they succeeded, and when they fail, obtaining as much information as possible as to why. An easy way to see where a package failed is to check the step's ExecutionResult property, which is the method used in my spDisplayPKGErrors stored procedure. As to why, this a little more difficult. Perhaps the best method is to use the step's GetExecutionErrorInfo method, but this will fail with a Type mismatch error. This is because by reference parameters must be exactly the same type as they are declared in the method. Unfortunately the OLE stored procedures do not support strong typing. On workaround is to create a simple COM object into which you pass a reference to the step, this can then call the GetExecutionErrorInfo correctly and return the error information for you. As this is yet another object to manage in your code, I generally avoid this when using the OLE stored procedures, and rely on the package error log to supply any further information. If this is insufficient, then you can always execute the package interactively via Enterprise Manager to get the step specific errors.



MS SQL Archives

Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 




Latest Forum Threads
MS SQL Forum
Topic By Replies Updated
SQL 2005: SSIS: Error using SQL Server credentials poverty 3 August 17th, 07:43 AM
Need help changing table contents nkawtg 1 August 17th, 03:02 AM
SQL Server Memory confifuration bhosalenarayan 2 August 14th, 05:33 AM
SQL Server Primary Key and a Unique Key katty.jonh 2 July 25th, 10:36 AM


















Thanks for your registration, follow us on our social networks to keep up-to-date