2014-11-14 4 views
0

Я использую SQL Server, В моей таблице у меня есть «NTEXT» column.the данные в NTEXT колонке, как показано нижеКак получить данные из столбца «NTEXT» в SQL сервере

"<?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 хотите получить heartBeat, name, recentLoad, runTime, running, startTime. может ли один помочь мне, как это сделать с помощью SQL запроса .. Заранее спасибо ......

ответ

0

смотри ниже:

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) 

Вы можете использовать любое выражение XQuery. См. http://msdn.microsoft.com/en-us/library/ms189075.aspx для получения более подробной информации.

Или вы можете обновить столбец таблицы, чтобы быть 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 
+0

Спасибо за response.I хочет знать, есть ли способ, используя только оператор выбора без использования «объявить». – steve

+0

В SQL 2014 я думаю, вы можете использовать 'cast (@str as XML)'. Какая версия SQL у вас есть? – SmartDev

+0

Вы должны изменить тип столбца с 'ntext' на' xml'. Тогда вы сможете сделать это в инструкции select. – SmartDev

Смежные вопросы