繁体   English   中英

遍历XML以在临时表中插入数据-SQL 2005

[英]Loop through an XML to insert data in a temp table - SQL 2005

我正在尝试执行此(独立)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)

似乎我需要创建一个表而不是使用临时表,是否有一种方法可以遍历XMl节点并将它们插入到临时表中(不使用游标)。

尝试这个:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM