我有这个XML对象:
<params>
<item>
<idtype>1</idtype>
<name>dsf2</name>
<value>2012-10-05 23:59:59</value>
</item>
<item>
<idtype>2</idtype>
<name>msm1</name>
<value>999</value>
</item>
</params>
如何将价值数据保存到如下表格中:
答案 0 :(得分:6)
DECLARE @XML XML = '<params>
<item>
<idtype>1</idtype>
<name>dsf2</name>
<value>2012-10-05 23:59:59</value>
</item>
<item>
<idtype>2</idtype>
<name>msm1</name>
<value>999</value>
</item>
</params>'
INSERT INTO [TableName] ([idtype], [name], [value])
SELECT
[idtype] = TypeNode.value('(idtype)[1]', 'int'),
[name] = TypeNode.value('(name)[1]', 'nvarchar(50)'),
[value] = TypeNode.value('(value)[1]', 'nvarchar(50)')
FROM
@XML.nodes('/params/item') AS XTbl(TypeNode)
顺便说一句:我假设您的表格定义如下:
CREATE TABLE [TableName](
[idtype] [int] NULL,
[name] [nvarchar](50) NULL,
[value] [nvarchar](50) NULL
)