Skip to content Skip to sidebar Skip to footer

Extracting SQL Statements From A SSIS/DTSX Package

I am looking for something to extract all the SQL queries present in my SSIS/DTSX package. But nothing is helping me till now. I already had a look at Microsoft.SqlServer.DTS API's

Solution 1:

The following Query is helpful to retrieve all the sql statements inside the SSIS package

;WITH XMLNAMESPACES ('www.microsoft.com/SqlServer/Dts' AS  
DTS,'www.microsoft.com/sqlserver/dts/tasks/sqltask' AS SQLTask) 

-- Query to Extract SQL Tasks with Name and SQL Statement 
SELECT Pkg.props.value('../../DTS:Property[@DTS:Name="ObjectName"]
[1]','varchar(MAX)') ObjectName, 
Pkg.props.value('(@SQLTask:SqlStatementSource)[1]', 'NVARCHAR(MAX)') AS 
SqlStatement FROM (select cast(pkgblob.BulkColumn as XML) pkgXML from 
openrowset(bulk 'Your DTS package with name and location Path',single_blob) 
as pkgblob) t CROSS APPLY pkgXML.nodes('//DTS:ObjectData//SQLTask:SqlTaskData') Pkg(props) 
UNION 

-- Query to Extract DTS Pipline task with Name and SqlCommand 

SELECT Pkg.props.value('../../../../DTS:Property[@DTS:Name="ObjectName"]
[1]','varchar(MAX)') ObjectName, 
Pkg.props.value('data(./properties/property[@name=''SqlCommand''])[1]', 
'varchar(max)') SqlStatement FROM(select cast(pkgblob.BulkColumn as XML) 
 pkgXML from openrowset(bulk 'Your DTS package with name and location 
 Path',single_blob) as pkgblob) t CROSS APPLY 
pkgXML.nodes('//DTS:Executable//pipeline//components//component') Pkg(props) 
WHERE Pkg.props.value('data(./properties/property[@name=''SqlCommand''])
[1]', 'varchar(max)') <>''

Solution 2:

There is another way.

You can create a custom log event. It is written about here:

enabling custom logging for ssis tasks

Then you just need to run the package and parse the log file that is created.

I'm not sure about DTS though but that should get all the SQL from expressions etc. in an SSIS package.


Post a Comment for "Extracting SQL Statements From A SSIS/DTSX Package"