Xpath查询 - 显示null的字段

时间:2015-11-09 21:29:52

标签: xpath sql-server-2008-r2

SQL Server 2008 R2

我有以下CTE和XPath查询,我遇到了问题。

IF OBJECT_ID('tempdb..#TempXML') IS NOT NULL
    DROP TABLE #TempXML;

WITH ConvertedToXML AS 
( 
SELECT top 500 xml_msg AsVarchar
,status [status]
,date_received 
,CAST(xml_msg AS XML) AS AsXml
FROM interface_master_out
order by date_received desc
)

SELECT
ConvertedToXML.AsXml.value('(//InstrumentID)[1]', 'varchar(max)') AS InstrumentID
,ConvertedToXML.AsXml.value('(//MessageType)[1]', 'Varchar(10)') as MessageType
,ConvertedToXML.AsXml.value('(//MessageID)[1]', 'Varchar(10)') as MessageID
,ConvertedToXML.AsXml.value('(//date_received)[1]', 'datetime') as date_received
,ConvertedToXML.AsXml.value('(//status)[1]', 'Varchar(10)') as [Status]
,ConvertedToXML.AsVarchar 
,ConvertedToXML.AsXml 
Into #TempXML
FROM ConvertedToXML
where ConvertedToXML.AsXml.value('(//InstrumentID)[1]', 'varchar(max)') in ('BA1254890001');

select * from #TempXML
drop table #TempXML

date_received和Status都是null,我无法弄清楚原因。我已将in ( )配对为单个值,但实际上只有30个。

1 个答案:

答案 0 :(得分:0)

一旦我玩了这么多,我实际上是非常容易的。以下是修订后的工作代码:

Use MyDatabase
IF OBJECT_ID('tempdb..#TempXML') IS NOT NULL
    DROP TABLE #TempXML;

WITH ConvertedToXML AS 
( 
SELECT top 50 xml_msg AsVarchar
,status Status
,date_received DateTime
,CAST(xml_msg AS XML) AS AsXml
FROM interface_master_out
order by date_received desc
)

SELECT
ConvertedToXML.AsXml.value('(//InstrumentID)[1]', 'varchar(max)') AS InstrumentID
,ConvertedToXML.AsXml.value('(//MessageType)[1]', 'Varchar(10)') as MessageType
,ConvertedToXML.AsXml.value('(//MessageID)[1]', 'Varchar(10)') as MessageID
,ConvertedToXML.Status
,ConvertedToXML.DateTime
,ConvertedToXML.AsVarchar
,ConvertedToXML.AsXml 
Into #TempXML
FROM ConvertedToXML
where ConvertedToXML.AsXml.value('(//InstrumentID)[1]', 'varchar(max)') = ('BA1254890001')

select * from #TempXML
drop table #TempXML