views:

255

answers:

1

I am trying to execute this (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)

It seems I require to create a Table instead of using Temp Table, Is there a way to loop through the XMl nodes and insert them into a Temp table (without using Cursors).

+1  A: 

Try this:

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]);
Ken