gpt4 book ai didi

sql-server - 从 SSIS/DTSX 包中提取 SQL 语句

转载 作者:行者123 更新时间:2023-12-02 13:43:43 30 4
gpt4 key购买 nike

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

我已经看过 Microsoft 的 Microsoft.SqlServer.DTS API。但他们正在直接提取一些查询。但是 DTS:variable 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;
}

最佳答案

下面的查询有助于检索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)') <>''

关于sql-server - 从 SSIS/DTSX 包中提取 SQL 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16357829/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com