2012-10-03 90 views
2

我有這個疑問如何在SQL Server 2005中讀取xml?

WITH XMLNAMESPACES (DEFAULT 'http://www.w3.org/2005/Atom') 
select 
cast(idsku as int) idsku, 
cast(entregado as int) entregado from #final1 final FOR XML AUTO, ROOT ('clientesxml'), ELEMENTS 

這回

<clientesxml xmlns="http://www.w3.org/2005/Atom"> 
    <final> 
    <idsku>191</idsku> 
    <entregado>159</entregado> 
    </final> 
</clientesxml> 

我的問題我怎麼創建一個存儲過程,它讀取XML返回,並將其轉換爲#tempTable

+0

您是否希望將數據插入回原來的格式? –

回答

1

以下是製作PROC時可以派上用場了幾個類似的方法:

-- Declare some xml 
declare @xml xml = '<clientesxml xmlns="http://www.w3.org/2005/Atom"> 
    <final> 
    <idsku>191</idsku> 
    <entregado>159</entregado> 
    </final> 
</clientesxml>' 

-- Shred the xml 
;with xmlnamespaces (default 'http://www.w3.org/2005/Atom') 
select x.Record.value('idsku[1]', 'int') as idsku 
    , x.Record.value('entregado[1]', 'int') as entregado 
from @xml.nodes('//clientesxml/final') as x (Record) 

-- Shred and insert the xml into a new temp table 
;with xmlnamespaces (default 'http://www.w3.org/2005/Atom') 
select x.Record.value('idsku[1]', 'int') as idsku 
    , x.Record.value('entregado[1]', 'int') as entregado 
into #tempTable 
from @xml.nodes('//clientesxml/final') as x (Record) 

-- Shred and insert the xml into an existing temp table 
;with xmlnamespaces (default 'http://www.w3.org/2005/Atom') 
insert into #tempTable (idsku, entregado) 
select x.Record.value('idsku[1]', 'int') as idsku 
    , x.Record.value('entregado[1]', 'int') as entregado 
from @xml.nodes('//clientesxml/final') as x (Record) 
+0

這插入我的所有記錄?或只有一個記錄? – angel

+0

包含在@xml中的所有記錄。一個'where'子句可以過濾它們。 –