查询 msdb.dbo.sysssispackages XML 中的特定值

dev*_*ter 4 xml sql t-sql sql-server sql-server-2008-r2

我正在尝试查询msdb.dbo.sysssispackages活动维护计划,但我无法理解如何查询列中的 XML packagedata。我在 SO 上发现了一些其他与 XML 相关的 SQL 问题,但到目前为止似乎没有什么完全符合我正在寻找的内容。

\n\n

XMLmsdb.dbo.sysssispackages.packagedata看起来像这样:

\n\n
<DTS:Executable DTS:ExecutableType="Microsoft.SqlServer.Management.DatabaseMaintenance.DbMaintenanceReindexTask, Microsoft.SqlServer.MaintenancePlanTasks, version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" DTS:ThreadHint="0">\n      <DTS:Property DTS:Name="TaskContact">Rebuild Index Task; Microsoft Corporation; Microsoft SQL Server v9; \xc2\xa9 2004 Microsoft Corporation; All Rights Reserved;http://www.microsoft.com/sql/support/default.asp;1</DTS:Property>\n      <DTS:Property DTS:Name="LocaleID">-1</DTS:Property>\n      <DTS:Property DTS:Name="ObjectName">Rebuild Index Task</DTS:Property>\n      <DTS:Property DTS:Name="DTSID">{E4A9C2C8-F24F-4AE3-AC3D-F8B6729F1126}</DTS:Property>\n      <DTS:Property DTS:Name="Description">Rebuild Index Task</DTS:Property>\n      <DTS:Property DTS:Name="CreationName">Microsoft.SqlServer.Management.DatabaseMaintenance.DbMaintenanceReindexTask, Microsoft.SqlServer.MaintenancePlanTasks, version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91</DTS:Property>\n      <DTS:EventHandler>\n        <DTS:Property DTS:Name="EventID">0</DTS:Property>\n        <DTS:Property DTS:Name="EventName">OnPostExecute</DTS:Property>\n        <DTS:Property DTS:Name="LocaleID">-1</DTS:Property>\n        <DTS:Property DTS:Name="DTSID">{263C94D7-6FEC-4A4A-9EA1-2557E3892E74}</DTS:Property>\n        <DTS:Property DTS:Name="CreationName">OnPostExecute</DTS:Property>\n        <DTS:Variable>\n          <DTS:Property DTS:Name="Namespace">System</DTS:Property>\n          <DTS:Property DTS:Name="IncludeInDebugDump">6789</DTS:Property>\n          <DTS:Property DTS:Name="ObjectName">Propagate</DTS:Property>\n          <DTS:Property DTS:Name="DTSID">{DB1B0594-A0F1-4C48-867E-C5A9C6BB9322}</DTS:Property>\n          <DTS:Property DTS:Name="Description">The propagate property of the event</DTS:Property>\n          <DTS:Property DTS:Name="CreationName" />\n          <DTS:VariableValue DTS:DataType="11">0</DTS:VariableValue>\n        </DTS:Variable>\n      </DTS:EventHandler>\n      <DTS:EventHandler>\n        <DTS:Property DTS:Name="EventID">0</DTS:Property>\n        <DTS:Property DTS:Name="EventName">OnPreExecute</DTS:Property>\n        <DTS:Property DTS:Name="LocaleID">-1</DTS:Property>\n        <DTS:Property DTS:Name="DTSID">{AD9E11AB-D830-41A7-8A38-FC2E89B71FD1}</DTS:Property>\n        <DTS:Property DTS:Name="CreationName">OnPreExecute</DTS:Property>\n        <DTS:Variable>\n          <DTS:Property DTS:Name="Namespace">System</DTS:Property>\n          <DTS:Property DTS:Name="IncludeInDebugDump">6789</DTS:Property>\n          <DTS:Property DTS:Name="ObjectName">Propagate</DTS:Property>\n          <DTS:Property DTS:Name="DTSID">{8A2A2399-1C88-4247-91E8-B87EB217F052}</DTS:Property>\n          <DTS:Property DTS:Name="Description">The propagate property of the event</DTS:Property>\n          <DTS:Property DTS:Name="CreationName" />\n          <DTS:VariableValue DTS:DataType="11">0</DTS:VariableValue>\n        </DTS:Variable>\n      </DTS:EventHandler>\n      <DTS:ObjectData>\n        <SQLTask:SqlTaskData xmlns:SQLTask="www.microsoft.com/sqlserver/dts/tasks/sqltask" SQLTask:Connection="{8E20FB62-A80C-4A72-9789-C250348EDB1B}" SQLTask:DatabaseSelectionType="3" SQLTask:ServerVersion="10" SQLTask:ExtendedLogging="False" SQLTask:LocalConnectionForLogging="Local server connection" SQLTask:TaskName="" SQLTask:IgnoreDatabasesInNotOnlineState="False" SQLTask:UseOriginalAmount="True" SQLTask:Percentage="-1" SQLTask:Sort="False" SQLTask:KeepOnline="False" SQLTask:SkipUnsupported="False">\n          <SQLTask:SelectedDatabases SQLTask:DatabaseName="AdventureWorks2008R2" />\n        </SQLTask:SqlTaskData>\n      </DTS:ObjectData>\n    </DTS:Executable>\n
Run Code Online (Sandbox Code Playgroud)\n\n
    \n
  1. 如何找到<DTS:Executable DTS:ExecutableType= 包含“Microsoft.SqlServer.Management.DatabaseMaintenance.DbMaintenanceReindexTask”的行,就像在 xml 第一行中找到的那样?
  2. \n
  3. 如何找到与搜索字符串<DTS:Property DTS:Name="ObjectName">A value here</DTS:Property> 匹配的行?
  4. \n
\n

Stu*_*tLC 5

您可以使用xquery来执行此操作。它有点难:

第1点:

;WITH XMLNAMESPACES 
(
    'www.microsoft.com/SqlServer/Dts' AS DTS
   ,'www.microsoft.com/SqlServer/Dts/Tasks' AS SQLTask)
SELECT
  Nodes.node.value('(@DTS:ExecutableType)[1]', 'varchar(100)') AS ExecutableType,
  Nodes.node.value('(DTS:Property[@DTS:Name="TaskContact"])[1]', 'varchar(100)') AS TaskContact
FROM
  (
    SELECT 
         CAST(CAST([packagedata] as varbinary(max)) as xml) PackageDataXml
    FROM      
         [msdb].[dbo].[sysssispackages]
  ) SysPackages
  CROSS APPLY 
    SysPackages.PackageDataXml.nodes('/DTS:Executable/DTS:Executable/DTS:Executable') Nodes(Node)
WHERE
  Nodes.node.value('@DTS:ExecutableType', 'varchar(100)') 
    LIKE 'Microsoft.SqlServer.Management.DatabaseMaintenance.DbMaintenanceReindexTask%';
Run Code Online (Sandbox Code Playgroud)

对于第 2 点,where 子句如下所示:

WHERE
  Nodes.node.value('(DTS:Property[@DTS:Name=''ObjectName''])[1]', 'varchar(50)') 
   = 'Rebuild Index Task';
Run Code Online (Sandbox Code Playgroud)

在MSDN 社交的帮助下

要点

  1. 需要将packagedata列通过 varbinary 转换为 xml
  2. 使用 Cross Apply 将“根节点选择器”xpath 应用于所有行
  3. 要寻址命名空间,请通过添加别名WITH XMLNAMESPACES
  4. Xquery 例如.value需要选择标量,因此(xpath)[1]