How to query XML as used in .dtsx (SSIS)

◇◆丶佛笑我妖孽 提交于 2021-02-08 03:42:31

问题


I have made an SSIS 2014 package (Project Deployment).

Here you see a (relevant) part of it's XML-content:

    <DTS:Executable DTS:refId="Package\Drop and Create statements voor tabellen\Drop and Create Classes_staging" DTS:CreationName="Microsoft.ExecuteSQLTask" DTS:Description="Execute SQL Task" DTS:DTSID="{60C174A8-D9C0-4F2A-B783-77753552BFCB}" DTS:ExecutableType="Microsoft.ExecuteSQLTask" DTS:LocaleID="-1" DTS:ObjectName="Drop and Create Classes_staging" DTS:TaskContact="Execute SQL Task; Microsoft Corporation; SQL Server 2014; © 2014 Microsoft Corporation; All Rights Reserved;http://www.microsoft.com/sql/support/default.asp;1" DTS:ThreadHint="7">
      <DTS:Variables />
      <DTS:ObjectData>
        <SQLTask:SqlTaskData xmlns:SQLTask="www.microsoft.com/sqlserver/dts/tasks/sqltask" SQLTask:Connection="{c25e8858-9d6b-424d-b231-3ae05aed0d4b}" SQLTask:SqlStatementSource="/****** Table [dbo].[Classes_staging] ******/&#xA;IF OBJECT_ID('[dbo].[Classes_staging]') IS NOT NULL&#xA;DROP TABLE [dbo].[Classes_staging];&#xA;GO&#xA;&#xA;CREATE TABLE [dbo].[Classes_staging](&#xA;&#x9;[Reference] [nvarchar](50) NULL) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]&#xA;&#xA;GO&#xA;&#xA;&#xA;" />
      </DTS:ObjectData>
    </DTS:Executable>

This file is imported into a table using OPENROWSET:

INSERT INTO Packages(XMLContent)
SELECT CONVERT(XML, BulkColumn) AS xContent
FROM OPENROWSET(BULK 'myFile.dtsx', SINGLE_BLOB) AS x;

Now I am trying to query it:

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

--Alle Executables
SELECT      Pkg.props.value('./@DTS:DTSID','nvarchar(max)') as [DTSID]
            ,Pkg.props.value('./@DTS:refId','nvarchar(max)') as [Executable]
            ,Pkg.props.value('./@DTS:ObjectName','nvarchar(max)') as [ObjectName]
            ,Pkg.props.value('./@DTS:Description','nvarchar(max)') as [Description]
            ,Pkg.props.value('./@DTS:ExecutableType','nvarchar(max)') as [ExecutableType]
            ,Pkg.props.value('./@SQLTask:SqlStatementSource','nvarchar(max)') as [SqlStatementSource]
FROM    (SELECT XMLContent AS pkgXML FROM packages) t

--CROSS APPLY pkgXML.nodes('//SQLTask:SqlTaskData') Pkg(props)
CROSS APPLY pkgXML.nodes('//DTS:Executable' ) Pkg(props)

I can either find all the SQLStatements used in the package OR I can find details concerning the executables. I can do this by swapping the two CROSS APPLY mentioned above. But I cannot do both... What am I missing here?


回答1:


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

--Alle Executables
SELECT      X.Exe.value('./@DTS:DTSID','nvarchar(max)') as [DTSID]
            ,X.Exe.value('./@DTS:refId','nvarchar(max)') as [Executable]
            ,X.Exe.value('./@DTS:ObjectName','nvarchar(max)') as [ObjectName]
            ,X.Exe.value('./@DTS:Description','nvarchar(max)') as [Description]
            ,X.Exe.value('./@DTS:ExecutableType','nvarchar(max)') as [ExecutableType]
            ,Y.Task.value('./@SQLTask:SqlStatementSource','nvarchar(max)') as [SqlStatementSource]

FROM    (SELECT XMLContent  AS pkgXML FROM packages) t

CROSS APPLY pkgXML.nodes('//DTS:Executable' ) X(Exe)
OUTER APPLY X.Exe.nodes('./DTS:ObjectData/SQLTask:SqlTaskData') Y(Task)



回答2:


Do another CROSS APPLY, which shred the XML on SqlTaskData, on the result of the first CROSS APPLY :

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

--Alle Executables
SELECT      Y.Task.value('./@DTS:DTSID','nvarchar(max)') as [DTSID]
            ,Y.Task.value('./@DTS:refId','nvarchar(max)') as [Executable]
            ,Y.Task.value('./@DTS:ObjectName','nvarchar(max)') as [ObjectName]
            ,Y.Task.value('./@DTS:Description','nvarchar(max)') as [Description]
            ,Y.Task.value('./@DTS:ExecutableType','nvarchar(max)') as [ExecutableType]
            ,X.Exe.value('./@SQLTask:SqlStatementSource','nvarchar(max)') as [SqlStatementSource]
FROM    (SELECT XMLContent AS pkgXML FROM packages) t

CROSS APPLY pkgXML.nodes('//DTS:Executable' ) X(Exe)
CROSS APPLY X.Exe.nodes('./DTS:ObjectData/SQLTask:SqlTaskData') Y(Task)


来源:https://stackoverflow.com/questions/43938001/how-to-query-xml-as-used-in-dtsx-ssis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!