1

连续的值被这样捕获

Item type    Item name 
1,2,3,        abc, def, ghi,

我的目标是将每个值插入到一个新表中,如下所示

Item type    Item name
1            abc
2            def
3            ghi

这是我现在可以捕获信息的唯一方法,因此将不胜感激任何帮助。请注意,我不是开发人员。谢谢

4

1 回答 1

1

试试这个——

询问:

DECLARE @table TABLE 
(
      a VARCHAR(200)
    , b VARCHAR(200)
)

INSERT INTO @table(a, b)
VALUES 
    ('1,2,3,', 'abc, def, ghi,'),
    ('1,2,3,4,', 'test1, test2, test3, test4,')

SELECT 
      [Item name] = LTRIM(data.p)
    , [Item type] = data.r
FROM (
    SELECT 
          p = p.value('(.)[1]', 'NVARCHAR(50)')
        , po = p.value('for $i in . return count(../*[. << $i])', 'int')
        , r = r.value('(.)[1]', 'NVARCHAR(50)')
        , ro = r.value('for $i in . return count(../*[. << $i])', 'int')
    FROM (
        SELECT 
              txml = CAST('<r><s>' + REPLACE(SUBSTRING(t.a, 1, LEN(t.a) - 1), ',', '</s>' + '<s>') + '</s></r>' AS XML)
            , kxml = CAST('<r><s>' + REPLACE(SUBSTRING(t.b, 1, LEN(t.b) - 1), ',', '</s>' + '<s>') + '</s></r>' AS XML)  
        FROM @table t
    ) d
    CROSS APPLY kxml.nodes('/r/s') t(p)
    CROSS APPLY txml.nodes('/r/s') k(r)
) data
WHERE data.po = data.ro

输出:

Item name   Item type
----------- ----------
abc         1
def         2
ghi         3
test1       1
test2       2
test3       3
test4       4
于 2013-05-13T14:05:49.610 回答