Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 读取XML列_Sql_Sql Server_Xml - Fatal编程技术网

Sql 读取XML列

Sql 读取XML列,sql,sql-server,xml,Sql,Sql Server,Xml,我的XML记录为SQL数据库中的一个字段,我试图从XML中读取一些值。例如,我尝试使用以下代码从XML获取ItemID,但没有返回结果 SELECT N.C.value('itemId[1]', 'int') ItemId FROM [isalesystemdb].[dbo].[Test_eBay_Keyword_Transaction] cross apply rawdata.nodes('/findItemsByKeywordsResponse/SearchResu

我的XML记录为SQL数据库中的一个字段,我试图从XML中读取一些值。例如,我尝试使用以下代码从XML获取ItemID,但没有返回结果

SELECT 
      N.C.value('itemId[1]', 'int') ItemId

  FROM [isalesystemdb].[dbo].[Test_eBay_Keyword_Transaction] 
  cross apply rawdata.nodes('/findItemsByKeywordsResponse/SearchResult/item') N(C)
谢谢你的帮助! 以下是XML的

<findItemsByKeywordsResponse> xmlns="http://www.ebay.com/marketplace/search/v1/services">
  <ack>Success</ack>
  <version>1.12.0</version>
  <timestamp>2013-09-03T01:10:07.967Z</timestamp>
  <searchResult count="100">
    <item>
      <itemId>231044911361</itemId>
      <title>3 X SNAKE REPELLER SOLAR POWER ULTRA SONIC LED PEST RODENT MULTI PULSE REPELLANT</title>
    </item>

</searchResult>
  <paginationOutput>
    <pageNumber>1</pageNumber>
    <entriesPerPage>100</entriesPerPage>
    <totalPages>3</totalPages>
    <totalEntries>270</totalEntries>
  </paginationOutput>
  <itemSearchURL>http://www.ebay.com.au/sch/i.html?_nkw=snake+repeller&amp;_ddo=1&amp;_ipg=100&amp;_pgn=1</itemSearchURL>
</findItemsByKeywordsResponse>'
xmlns=”http://www.ebay.com/marketplace/search/v1/services">
成功
1.12.0
2013-09-03T01:10:07.967Z
231044911361
3 X蛇驱避器太阳能超声波LED害虫啮齿动物多脉冲驱避器
1.
100
3.
270
http://www.ebay.com.au/sch/i.html?_nkw=snake+排斥剂_ddo=1&_ipg=100&_pgn=1
'
试试这个-

DECLARE @XML XML
SELECT @XML = '
<findItemsByKeywordsResponse xmlns="http://www.ebay.com/marketplace/search/v1/services">
    <ack>Success</ack>
    <version>1.12.0</version>
    <timestamp>2013-09-03T01:10:07.967Z</timestamp>
    <searchResult count="100">
        <item>
            <itemId>231044911361</itemId>
            <title>3 X SNAKE REPELLER SOLAR POWER ULTRA SONIC LED PEST RODENT MULTI PULSE REPELLANT</title>
        </item>
    </searchResult>
    <paginationOutput>
        <pageNumber>1</pageNumber>
        <entriesPerPage>100</entriesPerPage>
        <totalPages>3</totalPages>
        <totalEntries>270</totalEntries>
    </paginationOutput>
    <itemSearchURL>http://www.ebay.com.au/sch/i.html?_nkw=snake+repeller&amp;_ddo=1&amp;_ipg=100&amp;_pgn=1</itemSearchURL>
</findItemsByKeywordsResponse>'

;WITH XMLNAMESPACES (DEFAULT 'http://www.ebay.com/marketplace/search/v1/services')
SELECT itemId = t.c.value('itemId[1]', 'BIGINT')
FROM @XML.nodes('/findItemsByKeywordsResponse/searchResult/item') t(c)
DECLARE@XML
选择@XML=
成功
1.12.0
2013-09-03T01:10:07.967Z
231044911361
3 X蛇驱避器太阳能超声波LED害虫啮齿动物多脉冲驱避器
1.
100
3.
270
http://www.ebay.com.au/sch/i.html?_nkw=snake+排斥剂_ddo=1&_ipg=100&_pgn=1
'
;使用XMLNAMESPACES(默认值'http://www.ebay.com/marketplace/search/v1/services')
选择itemId=t.c.value('itemId[1],'BIGINT'))
来自@XML.nodes('/findItemsByKeywordsResponse/searchResult/item')t(c)
输出-


XML区分大小写。将
/SearchResult/
替换为
/SearchResult/

虽然答案已被接受,但对您为解决问题所做的工作进行某种解释是非常有用的。是的,这有助于当前用户了解这个确切的场景,但除非其他人拥有完全相同的XML,否则不太可能让其他人受益。例如,如果没有其他答案,用户可能没有注意到案例问题。就像那句老话,给一个人一条鱼,你就喂他一天;教他如何捕鱼,你就可以养活他一辈子。