Domanda

Sto cercando di eseguire questo (standalone) SQL:

Declare @test XML
SET @test = '<Products><ProductId>1</ProductId><ProductId>2</ProductId></Products>'
DECLARE @Temp TABLE(        
    ProductId NVARCHAR(10)
   )  
INSERT INTO @Temp(ProductId)
SELECT tab.col.value('./ProductId[1]','NVARCHAR(10)') AS 'ProductId'
FROM @test
CROSS APPLY
xml_data.nodes('//Products') AS tab(col)

Sembra ho bisogno di creare una tabella invece di utilizzare Temp Tavolo, C'è un modo per scorrere i nodi XML e inserirli in una tabella temporanea (senza l'utilizzo di cursori).

È stato utile?

Soluzione

Prova questo:

declare @testXml as xml;
set @testXml = '<products><product productId="1"/><product productId="2"/></products>';
declare @temp table(ProductId nVarChar(10));
insert into @temp(ProductId)
select [xmlData].[Col].value('./@productId', 'nVarChar(10)') as [ProductId]
from @testXml.nodes('/products/product') as [xmlData]([Col]);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top