0

我将 2 个分号分隔的字符串与其他具有简单值的变量一起传递给存储过程。这些分号分隔的值实际上需要拆分并作为不同的行插入。两个分号分隔的值不相关,因此可以以任何顺序一起插入到 2 个不同的字段中。拆分是通过使用 Collapse | 完成的。语言:c select CAST(DATA AS INT) as id from SplitString('30:40:50',':') 但我想将这两个拆分组合起来,这样我就可以将它们一起传递到单个插入语句中。

例如,我有 string1 '30:40:50' 和 string2 '23,58,48,60' 和一些值 @id=101,datecreated...

现在我想将这些值插入到一个看起来像这样的表中,

101      30      23      Jun 25 2013 12:22PM

101      40      58      Jun 25 2013 12:22PM

101      50      48      Jun 25 2013 12:22PM

101      null    60      Jun 25 2013 12:22PM

任何字符串都可以更长或更小或相等。

我无法弄清楚这样做的正确方法

谁能帮我解决这个问题。

4

1 回答 1

1

试试这个——

使用 LEFT JOIN 查询:

DECLARE 
       @s1 VARCHAR(50)
     , @s2 VARCHAR(50)

SELECT 
       @s1 = '30:40:50'
     , @s2 = '23,58,48,60'

SELECT 
       record_id = 104
     , t2.id1
     , t3.id2
     , dt = CONVERT(VARCHAR(20), GETDATE(), 100)
FROM [master].dbo.spt_values t
LEFT JOIN (
     SELECT 
            id1 = t.c.value('@s', 'INT')
          , rn = ROW_NUMBER() OVER (ORDER BY 1/0)
     FROM (
         SELECT field = CAST('<t s = "' + 
               REPLACE(
                      @s1 + ':'
                    , ':'
                    , '" /><t s = "') + '" />' AS XML) 
     ) d
     CROSS APPLY field.nodes('/t') t(c)
     WHERE t.c.value('@s', 'VARCHAR(5)') != ''
) t2 ON t.number = t2.rn
LEFT JOIN (
     SELECT 
            id2 = t.c.value('@s', 'INT')
          , rn = ROW_NUMBER() OVER (ORDER BY 1/0)
     FROM (
         SELECT field = CAST('<t s = "' + 
               REPLACE(
                      @s2 + ','
                    , ','
                    , '" /><t s = "') + '" />' AS XML) 
     ) d
     CROSS APPLY field.nodes('/t') t(c)
     WHERE t.c.value('@s', 'VARCHAR(5)') != ''
) t3 ON t.number = t3.rn
WHERE (t2.id1 IS NOT NULL OR t3.id2 IS NOT NULL)
   AND t.[type] = 'p'

使用 FULL OUTER JOIN 查询:

SELECT 
       record_id = 104
     , t2.id1
     , t3.id2
     , dt = CONVERT(VARCHAR(20), GETDATE(), 100)
FROM (
     SELECT 
            id1 = t.c.value('@s', 'INT')
          , rn = ROW_NUMBER() OVER (ORDER BY 1/0)
     FROM (
         SELECT field = CAST('<t s = "' + 
               REPLACE(
                      @s1 + ':'
                    , ':'
                    , '" /><t s = "') + '" />' AS XML) 
     ) d
     CROSS APPLY field.nodes('/t') t(c)
     WHERE t.c.value('@s', 'VARCHAR(5)') != ''
) t2
FULL OUTER JOIN (
     SELECT 
            id2 = t.c.value('@s', 'INT')
          , rn = ROW_NUMBER() OVER (ORDER BY 1/0)
     FROM (
         SELECT field = CAST('<t s = "' + 
               REPLACE(
                      @s2 + ','
                    , ','
                    , '" /><t s = "') + '" />' AS XML) 
     ) d
     CROSS APPLY field.nodes('/t') t(c)
     WHERE t.c.value('@s', 'VARCHAR(5)') != ''
) t3 ON t2.rn = t3.rn

输出:

record_id   id1         id2         dt
----------- ----------- ----------- --------------------
104         30          23          Jun 25 2013  3:50PM
104         40          58          Jun 25 2013  3:50PM
104         50          48          Jun 25 2013  3:50PM
104         NULL        60          Jun 25 2013  3:50PM
于 2013-06-25T12:47:26.283 回答