2017-02-22 106 views
0

我想从表中读取一个varchar列,但此列实际上是Xml,所以我将该值转换为XML并试图从那里获取值。获取null尝试读取XML列值SQL

问题是我总是变空。这是我的代码:

declare @Greeting xml = (select CAST(sg.Greeting as xml) from AnsService.Ans.SiteGroup sg with (nolock) where sg.SiteGroupNum = 2032) 

select 
    sg.AnswerAs, 
    (select xmlData.Col.value('.', 'varchar(max)') from @Greeting.nodes('//Section/Paragraph/Run') xmlData(col)) as Greeting 
from AnsService.Ans.SiteGroup sg with (nolock) 
where sg.SiteGroupNum = 2032 

转换后的XML值是:

<Section xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xml:space="preserve> 
    <Paragraph> 
    <Run>Thank you for calling Intelli-tec Security Services. This is [OpFirst] speaking, how may I help you?</Run> 
    </Paragraph> 
</Section> 

任何人都可以帮助我在这里找出我的问题,谢谢

回答

1

有几个缺陷:

  • bad habit to kick: NOLOCK everywhere
  • 如果XML sho w我们是完整的,不需要APPLY.nodes()
  • 不要在“部分”之前使用双重//。这会触发深层搜索并在XML中的任何位置查找任何元素<Section>
  • 您的XML定义了默认名称空间。您必须使用它或使用通配符
  • 您应该以适当的类型存储XML。不需要强制转换(如select CAST(sg.Greeting as xml
  • 看起来,就好像这个XML存储在您正在读取其余的同一张表中一样。有(可能)没有必要读入一个变量第一

这个试试这个:

DECLARE @Greeting XML= 
N'<Section xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xml:space="preserve"> 
    <Paragraph> 
    <Run>Thank you for calling Intelli-tec Security Services. This is [OpFirst] speaking, how may I help you?</Run> 
    </Paragraph> 
</Section>'; 

--declared namespace 
SELECT 
    @Greeting.value('declare namespace ns1="http://schemas.microsoft.com/winfx/2006/xaml/presentation"; 
        (/ns1:Section/ns1:Paragraph/ns1:Run/text())[1]','nvarchar(max)') Greeting; 

--predefined namespace 
WITH XMLNAMESPACES(DEFAULT 'http://schemas.microsoft.com/winfx/2006/xaml/presentation') 
SELECT 
    @Greeting.value('(/Section/Paragraph/Run/text())[1]','nvarchar(max)') Greeting; 

--no namespace but wildcards 
SELECT 
    @Greeting.value('(/*:Section/*:Paragraph/*:Run/text())[1]','nvarchar(max)') Greeting; 

如果我的魔晶球是正常工作,你需要像这样

select 
    sg.AnswerAs, 
    CAST(sg.Greeting AS XML).value('declare namespace ns1="http://schemas.microsoft.com/winfx/2006/xaml/presentation"; 
            (/ns1:Section/ns1:Paragraph/ns1:Run/text())[1]','nvarchar(max)') AS Greeting 
from AnsService.Ans.SiteGroup sg with (nolock) 
where sg.SiteGroupNum = 2032 
+0

非常感谢@Shnugo你是最好的,你的回答是绝对正确的。我非常感谢你对此的帮助。所有的建议都是可悲的,因为数据库不是我的,所以我必须以实施的方式使用它。非常感谢。 –