查询msdb.dbo.sysssispackages XML以获取特定值

时间:2014-01-23 01:57:27

标签: sql sql-server xml tsql sql-server-2008-r2

我正在尝试查询msdb.dbo.sysssispackages有效的维护计划,而我无法理解如何在packagedata列中查询XML。我在SO上发现了一些与XML相关的SQL问题,但到目前为止,似乎没有什么比我想要的更合适了。

msdb.dbo.sysssispackages.packagedata中的XML如下所示:

<DTS:Executable DTS:ExecutableType="Microsoft.SqlServer.Management.DatabaseMaintenance.DbMaintenanceReindexTask, Microsoft.SqlServer.MaintenancePlanTasks, version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" DTS:ThreadHint="0">
      <DTS:Property DTS:Name="TaskContact">Rebuild Index Task; Microsoft Corporation; Microsoft SQL Server v9; © 2004 Microsoft Corporation; All Rights Reserved;http://www.microsoft.com/sql/support/default.asp;1</DTS:Property>
      <DTS:Property DTS:Name="LocaleID">-1</DTS:Property>
      <DTS:Property DTS:Name="ObjectName">Rebuild Index Task</DTS:Property>
      <DTS:Property DTS:Name="DTSID">{E4A9C2C8-F24F-4AE3-AC3D-F8B6729F1126}</DTS:Property>
      <DTS:Property DTS:Name="Description">Rebuild Index Task</DTS:Property>
      <DTS:Property DTS:Name="CreationName">Microsoft.SqlServer.Management.DatabaseMaintenance.DbMaintenanceReindexTask, Microsoft.SqlServer.MaintenancePlanTasks, version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91</DTS:Property>
      <DTS:EventHandler>
        <DTS:Property DTS:Name="EventID">0</DTS:Property>
        <DTS:Property DTS:Name="EventName">OnPostExecute</DTS:Property>
        <DTS:Property DTS:Name="LocaleID">-1</DTS:Property>
        <DTS:Property DTS:Name="DTSID">{263C94D7-6FEC-4A4A-9EA1-2557E3892E74}</DTS:Property>
        <DTS:Property DTS:Name="CreationName">OnPostExecute</DTS:Property>
        <DTS:Variable>
          <DTS:Property DTS:Name="Namespace">System</DTS:Property>
          <DTS:Property DTS:Name="IncludeInDebugDump">6789</DTS:Property>
          <DTS:Property DTS:Name="ObjectName">Propagate</DTS:Property>
          <DTS:Property DTS:Name="DTSID">{DB1B0594-A0F1-4C48-867E-C5A9C6BB9322}</DTS:Property>
          <DTS:Property DTS:Name="Description">The propagate property of the event</DTS:Property>
          <DTS:Property DTS:Name="CreationName" />
          <DTS:VariableValue DTS:DataType="11">0</DTS:VariableValue>
        </DTS:Variable>
      </DTS:EventHandler>
      <DTS:EventHandler>
        <DTS:Property DTS:Name="EventID">0</DTS:Property>
        <DTS:Property DTS:Name="EventName">OnPreExecute</DTS:Property>
        <DTS:Property DTS:Name="LocaleID">-1</DTS:Property>
        <DTS:Property DTS:Name="DTSID">{AD9E11AB-D830-41A7-8A38-FC2E89B71FD1}</DTS:Property>
        <DTS:Property DTS:Name="CreationName">OnPreExecute</DTS:Property>
        <DTS:Variable>
          <DTS:Property DTS:Name="Namespace">System</DTS:Property>
          <DTS:Property DTS:Name="IncludeInDebugDump">6789</DTS:Property>
          <DTS:Property DTS:Name="ObjectName">Propagate</DTS:Property>
          <DTS:Property DTS:Name="DTSID">{8A2A2399-1C88-4247-91E8-B87EB217F052}</DTS:Property>
          <DTS:Property DTS:Name="Description">The propagate property of the event</DTS:Property>
          <DTS:Property DTS:Name="CreationName" />
          <DTS:VariableValue DTS:DataType="11">0</DTS:VariableValue>
        </DTS:Variable>
      </DTS:EventHandler>
      <DTS:ObjectData>
        <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">
          <SQLTask:SelectedDatabases SQLTask:DatabaseName="AdventureWorks2008R2" />
        </SQLTask:SqlTaskData>
      </DTS:ObjectData>
    </DTS:Executable>
  1. 如何找到<DTS:Executable DTS:ExecutableType= 包含“Microsoft.SqlServer.Management.DatabaseMaintenance.DbMaintenanceReindexTask”的行,就像在xml的第一行中找到的那样?
  2. 如何找到<DTS:Property DTS:Name="ObjectName">A value here</DTS:Property> 匹配搜索字符串的行?

1 个答案:

答案 0 :(得分:4)

您可以使用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%';

对于Point 2,where子句是这样的:

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

MSDN Social

的帮助下

突出点

  1. 需要通过varbinary将packagedata列强制转换为xml
  2. 使用“交叉应用”将“根节点选择器”xpath应用于所有行
  3. 要解决名称空间,请通过WITH XMLNAMESPACES
  4. 添加别名
  5. .value之类的Xquery需要选择标量,因此(xpath)[1]