[XML 插入]xml 多列插入表变量

 DECLARE  @t Table (c1 int,c2 int)
    DECLARE @x XML
    SET @x = CONVERT(XML,
    '<items>
    <item c1="' + REPLACE(REPLACE('1-4,2-5,3-6', ',', '"/><item c1="'),'-','" c2="') + '"/>
</items>')
  INSERT INTO @t 
        SELECT 
            x.item.value('@c1[1]', 'INT'),
            x.item.value('@c2[1]', 'INT') 
        FROM @x.nodes('//items/item') AS x(item)
 SELECT * FROM  @t
 DECLARE  @t Table (c1 int)
    DECLARE @x XML
    SET @x = CONVERT(XML,
    '<items>
    <item c1="' + REPLACE('1,2,3', ',', '"/><item c1="') + '"/>
</items>')
  INSERT INTO @t 
        SELECT 
            x.item.value('@c1[1]', 'INT')
        FROM @x.nodes('//items/item') AS x(item)
 SELECT * FROM  @t

 

posted @ 2019-04-30 14:58  NCat  阅读(222)  评论(0)    收藏  举报