2

我有一个包含 xml 的 ntext 类型列的表。我尝试应用许多示例来说明如何从特定节点的 xml 中提取公司名称的值,但仍然出现语法错误。下面是我所做的,除了用我的 select 语句替换了实际的 xml 输出

DECLARE @companyxml xml

    SET @companyxml = 
    '<Home>
      <slideshowImage1>1105</slideshowImage1>
      <slideshowImage2>1106</slideshowImage2>
      <slideshowImage3>1107</slideshowImage3>
      <slideshowImage4>1108</slideshowImage4>
      <slideshowImage5>1109</slideshowImage5>
      <bottomNavImg1>1155</bottomNavImg1>
      <bottomNavImg2>1156</bottomNavImg2>
      <bottomNavImg3>1157</bottomNavImg3>
      <pageTitle>Acme Capital Management |Homepage</pageTitle>
      <metaKeywords><![CDATA[]]></metaKeywords>
      <metaDescription><![CDATA[]]></metaDescription>
      <companyName>Acme Capital Management</companyName>
      <logoImg>1110</logoImg>
      <pageHeader></pageHeader>
    </Home>'

SELECT c.value ('companyName','varchar(1000)') AS companyname 
FROM @companyxml.nodes('/Home') AS c

出于某种原因,select c.value 语句存在我无法弄清楚的语法问题。在 SSMS 中悬停时,它说“找不到列“c”或用户定义的函数或聚合“c.value”,或者名称不明确。

任何有关语法的帮助将不胜感激。

4

1 回答 1

3

试试这个

 DECLARE @companyxml xml

SET @companyxml = 
'<Home>
  <slideshowImage1>1105</slideshowImage1>
  <slideshowImage2>1106</slideshowImage2>
  <slideshowImage3>1107</slideshowImage3>
  <slideshowImage4>1108</slideshowImage4>
  <slideshowImage5>1109</slideshowImage5>
  <bottomNavImg1>1155</bottomNavImg1>
  <bottomNavImg2>1156</bottomNavImg2>
  <bottomNavImg3>1157</bottomNavImg3>
  <pageTitle>Acme Capital Management Homepage</pageTitle>
  <metaKeywords>CDATA</metaKeywords>
  <metaDescription>CDATA</metaDescription>
  <companyName>Acme Capital Management</companyName>
  <logoImg>1110</logoImg>
  <pageHeader></pageHeader>
</Home>'


DECLARE @Result AS varchar(50)

SET @result = @companyxml.value('(/Home/companyName/text())[1]','varchar(50)')

SELECT @result
于 2013-01-22T18:17:46.300 回答