简体   繁体   English

如何从SQL Server中的“ ntext”列获取数据

[英]How to get the data from “ntext” column in sql server

I am using sql server, In my table I have an "ntext" column.the data in ntext column is as below 我正在使用sql server,在我的表中有一个“ ntext”列。ntext列中的数据如下

"<?xml version="1.0" encoding="UTF-8"?>
<processEngine id="5000001" instanceName="bg-claritysql.excers">
   <controller heartBeat="2014-11-14T19:35:57"/>
   <loader heartBeat="2014-11-14T19:35:57" queueLength="1"/>
   <conditionWaitList queueLength="1"/>
   <retryWaitList queueLength="0"/>
   <actionWaitList queueLength="0"/>
   <PreConditionPipelineManager load="3.451246679588729E-7" noOfPipelines="2" queueLength="0"
                                recentLoad="8.510423949066926E-6">
           <pipeline heartBeat="2014-10-11T04:14:17" index="1" load="1.0022981644497761E-9"
                name="Pre Condition Pipeline 1"
                recentLoad="1.0022981684681825E-9"
                runTime="3"
                running="false"
                startTime="2014-10-11T04:14:17"/>
   </PreConditionPipelineManager>
   <PostConditionTransitionPipelineManager load="8.273414600907745E-7" noOfPipelines="3" queueLength="0"
  </processEngine>"

I want to get the heartBeat,name,recentLoad,runTime,running,startTime values. 我想获取heartBeat,name,recentLoad,runTime,running,startTime值。 can any one help me how to do this using sql query.. Thanks in advance...... 谁能帮助我如何使用sql查询来做到这一点。.在此先感谢......

See below: 见下文:

declare @str nvarchar(max) = '<processEngine id="5000001" instanceName="bg-claritysql.excers">
   <controller heartBeat="2014-11-14T19:35:57"/>
   <loader heartBeat="2014-11-14T19:35:57" queueLength="1"/>
   <conditionWaitList queueLength="1"/>
   <retryWaitList queueLength="0"/>
   <actionWaitList queueLength="0"/>
   <PreConditionPipelineManager load="3.451246679588729E-7" noOfPipelines="2" queueLength="0" recentLoad="8.510423949066926E-6">
           <pipeline heartBeat="2014-10-11T04:14:17" index="1" load="1.0022981644497761E-9"
                name="Pre Condition Pipeline 1"
                recentLoad="1.0022981684681825E-9"
                runTime="3"
                running="false"
                startTime="2014-10-11T04:14:17"/>
            <pipeline heartBeat="2014-10-11T04:14:17" index="1" load="1.0022981644497761E-9"
                name="Pre Condition Pipeline 1"
                recentLoad="1.0022981684681825E-9"
                runTime="3"
                running="false"
                startTime="2014-10-11T04:14:17"/>
   </PreConditionPipelineManager>
   <PostConditionTransitionPipelineManager load="8.273414600907745E-7" noOfPipelines="3" queueLength="0" />
  </processEngine>'

declare @xml xml = @str

select 
    t.value('(./controller/@heartBeat)[1]', 'datetime') as HeartBeat,
    t.value('(./PreConditionPipelineManager/@recentLoad)[1]', 'float') as RecentLoad,
    t.value('(./PreConditionPipelineManager/pipeline/@recentLoad)[2]', 'float') as PipelineRecentLoad,
    t.value('(./PreConditionPipelineManager/pipeline/@running)[1]', 'bit') as PipelineRunning,
    t.value('(./PreConditionPipelineManager/pipeline/@startTime)[1]', 'datetime') as PipelineStartTime
from 
    @xml.nodes('processEngine') as a(t)

You can use any XQuery expression. 您可以使用任何XQuery表达式。 See http://msdn.microsoft.com/en-us/library/ms189075.aspx for more details. 有关更多详细信息,请参见http://msdn.microsoft.com/zh-cn/library/ms189075.aspx

Or you can update your table column to be of xml type instead of ntext and use a query like: 或者,您可以将表列更新为xml类型而不是ntext并使用类似以下的查询:

select 
    xmlcolumn.value('(./processEngine/controller/@heartBeat)[1]', 'datetime') as HeartBeat,
    xmlcolumn.value('(./processEngine/PreConditionPipelineManager/@recentLoad)[1]', 'float') as RecentLoad,
    xmlcolumn.value('(./processEngine/PreConditionPipelineManager/pipeline/@recentLoad)[2]', 'float') as PipelineRecentLoad,
    xmlcolumn.value('(./processEngine/PreConditionPipelineManager/pipeline/@running)[1]', 'bit') as PipelineRunning,
    xmlcolumn.value('(./processEngine/PreConditionPipelineManager/pipeline/@startTime)[1]', 'datetime') as PipelineStartTime
from 
    xmltable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM