从SSIS / DTSX包中提取SQL语句

时间:2013-05-03 11:24:43

标签: sql-server visual-studio ssis

我正在寻找能够提取我的SSIS / DTSX包中存在的所有SQL查询的东西。但到目前为止没有什么能帮助我。

我已经看过Microsoft的Microsoft.SqlServer.DTS API了。但他们正在直接提取一些问题。但是 DTS:变量 TAG中存在的查询,它们不会被提取。

我想在.Net框架中找到一些东西。因为我需要使用输出来执行其他任务。我正在使用C#。

示例代码如下。不解决所有情况

// this function takes the list of task hosts as input
// and gives all the queries present in taskhosts.

public static string ExtractQueriesFromTasks(List<TaskHost> Tasks)
{
    string src_query = "";
    foreach (TaskHost executable in Tasks)
    {
        DtsContainer Seq_container = (DtsContainer)executable;
        if (executable.InnerObject.GetType().Name == "ExecuteSQLTask")
        {
            ExecuteSQLTask sqlTask = (ExecuteSQLTask)executable.InnerObject;
            string src_query2 = sqlTask.SqlStatementSource;

            src_query = src_query + "\n" + src_query2.ToUpper();
        }
        if (executable.InnerObject.GetType().Name == "__ComObject")
        {
            IDTSPipeline100 sqlTask = (IDTSPipeline100)executable.InnerObject;
            Console.WriteLine(Microsoft.VisualBasic.Information.TypeName(executable.InnerObject));
            //ExecuteSQLTask sqlTask = (ExecuteSQLTask)executable.InnerObject;
            //string src_query2 = sqlTask.SqlStatementSource;

            //src_query = src_query + "\n" + src_query2.ToUpper();
        }


        if (executable.InnerObject.GetType().Name == "ScriptTask")
        {
            ExecuteSQLTask sqlTask = (ExecuteSQLTask)executable.InnerObject;
            string src_query2 = sqlTask.SqlStatementSource;

            src_query = src_query + "\n" + src_query2.ToUpper();
        }
    }
    return src_query;
}

2 个答案:

答案 0 :(得分:3)

以下查询有助于检索SSIS包中的所有sql语句

;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)') <>''

答案 1 :(得分:0)

还有另一种方式。

您可以创建自定义日志事件。它写在这里:

enabling custom logging for ssis tasks

然后你只需要运行包并解析创建的日志文件。

我不确定DTS但是应该从SSIS包中的表达式等获取所有SQL。