从sql中选择

时间:2011-04-25 13:52:40

标签: sql xml

<suggestions>
 <suggestion>
  <description>plate.</description> 
 </suggestion>
 <suggestion>
 <description>snack.</description> 
 </suggestion>
</suggestions>

DECLARE @Suggestions TABLE (
     [Description] VARCHAR(800)
   )

我的存储过程中的XML类型变量中包含xml 如何在描述标签之间插入每个文本 在表@Suggestions

2 个答案:

答案 0 :(得分:4)

试试这个:

DECLARE @input XML = '<suggestions>
 <suggestion>
  <description>plate.</description> 
 </suggestion>
 <suggestion>
 <description>snack.</description> 
 </suggestion>
</suggestions>'

DECLARE @Suggestions TABLE ([Description] VARCHAR(800))

INSERT INTO @Suggestions
    SELECT
        Sugg.value('(description)[1]', 'varchar(800)')
    FROM
        @input.nodes('/suggestions/suggestion') AS Tbl(Sugg)

SELECT * FROM @Suggestions

@input.nodes()表达式基本上将每个<suggestion>节点变为名为Tbl(Sugg)的“伪”表中的一行。从那些“行”(每个基本上是另一个XML片段),然后选择你感兴趣的值 - 这里是XML片段中<description> XML元素的内容。

答案 1 :(得分:0)

您可以使用LINQ-to-XML获取所有建议,然后您可以将该数据插入表中。