如何将大量列数据插入临时表?

时间:2016-04-12 10:31:51

标签: sql sql-server sql-server-2008-r2

我需要连续插入13列数据,数据存储在一个变量中,数据的每一部分都用逗号分隔。

declare @cldt varchar(max)="SINDA,--,--,--,--,--,--,--,--,30.00,--,--,--";

我有以下临时表

declare @TempTab table (
    idx int identity(1,1),
    Component varchar(200),
    Month1 varchar(max), 
    Month2 varchar(max),
    Month3 varchar(max),
    Month4 varchar(max),
    Month5 varchar(max),
    Month6 varchar(max),
    Month7 varchar(max),
    Month8 varchar(max),
    Month9 varchar(max),
    Month10 varchar(max),
    Month11 varchar(max),
    Month12 varchar(max)
);

如何将上述值插入此临时表?

我尝试过以下代码但没有用:

insert into @TempTab select @cldt

2 个答案:

答案 0 :(得分:2)

您正试图将单个值放入多个字段中,因此我担心它无法正常工作。

在完成此操作之前,您需要先将@cldt变量中的值拆分。

下面的脚本应该做你想要的,尽管有更好的(更快的)解决方案。我已经添加了评论,因此您可以浏览并了解正在发生的事情,以便您可以根据需要进行修改。

/* What's our values */
declare 
        @cldt varchar(max)='SINDA,--,--,--,--,--,--,--,--,30.00,--,--,--'
    ,   @delimiter  CHAR(1) = ',';

/* Add final delimiter to get last value */
SET @cldt = @cldt + ',';

    declare @TempTab table 
(
    idx int identity(1,1)
    ,Component varchar(200)
    ,Month1 varchar(max)
    , Month2 varchar(max)
    ,Month3 varchar(max)
    ,Month4 varchar(max)
    ,Month5 varchar(max)
    ,Month6 varchar(max)
    ,Month7 varchar(max)
    ,Month8 varchar(max)
    ,Month9 varchar(max)
    ,Month10 varchar(max)
    ,Month11 varchar(max)
    ,Month12 varchar(max)
);

/* Need a holding table in the meantime */
declare @TempTab_holding table 
(
        [id]                INT 
    ,   [original_text]     VARCHAR(100)
    ,   [remaining_text]    VARCHAR(100)
    ,   [values]            VARCHAR(100)
);

/* Split the string out */
WITH
    delimiting_cte
(
    ID,
    original_text,
    remaining_text,
    delimited_text
)
AS
(
    -- Anchor stmt : 
    SELECT
        CAST( 1 as SMALLINT ),
        @cldt,
        RIGHT( @cldt, ( LEN( @cldt ) - CHARINDEX( @delimiter, @cldt ) ) ),  --  remaining_text
        SUBSTRING( @cldt, 1, CHARINDEX( @delimiter, @cldt ) - 1 )                       --  delimited_text

    UNION ALL

    -- Recursive stmt :
    -- Recursive CTE to iteratively remove each delimited value & put in own row...
    SELECT
        CAST( c.ID + 1 as SMALLINT ),
        c.original_text,
        RIGHT( c.remaining_text, ( LEN( remaining_text ) - CHARINDEX( @delimiter, c.remaining_text ) ) ),   --  remaining_text
        SUBSTRING( c.remaining_text, 0, CHARINDEX( @delimiter, c.remaining_text )  )                        --  delimited_text
    FROM delimiting_cte as [c]
    WHERE
        -- Until no delimiter left in the [remaining_text] column...
        remaining_text like '%['+@delimiter+']%'
)
INSERT INTO @TempTab_holding
SELECT
    ID,
    original_text,
    remaining_text,
    delimited_text
FROM delimiting_cte as [c];

/* Finally populate the values */
INSERT INTO @TempTab
(Component, Month1, Month2, Month3, Month4, Month5, Month6, Month7, Month8, Month9, Month10, Month11, Month12 )
SELECT [Component] = (SELECT [values] FROM @TempTab_holding WHERE [id] = 1)
    , [Month1] = (SELECT [values] FROM @TempTab_holding WHERE [id] = 2)
    , [Month2] = (SELECT [values] FROM @TempTab_holding WHERE [id] = 3)
    , [Month3] = (SELECT [values] FROM @TempTab_holding WHERE [id] = 4)
    , [Month4] = (SELECT [values] FROM @TempTab_holding WHERE [id] = 5)
    , [Month5] = (SELECT [values] FROM @TempTab_holding WHERE [id] = 6)
    , [Month6] = (SELECT [values] FROM @TempTab_holding WHERE [id] = 7)
    , [Month7] = (SELECT [values] FROM @TempTab_holding WHERE [id] = 8)
    , [Month8] = (SELECT [values] FROM @TempTab_holding WHERE [id] = 9)
    , [Month9] = (SELECT [values] FROM @TempTab_holding WHERE [id] = 10)
    , [Month10] = (SELECT [values] FROM @TempTab_holding WHERE [id] = 11)
    , [Month11] = (SELECT [values] FROM @TempTab_holding WHERE [id] = 12)
    , [Month12] = (SELECT [values] FROM @TempTab_holding WHERE [id] = 13);

SELECT * FROM @TempTab;

答案 1 :(得分:2)

DECLARE @xml xml

SELECT @xml = ('<r>' + REPLACE(@cldt,',','</r><r>') + '</r>')

INSERT INTO @TempTab 
SELECT  t.v.value('r[1]', 'varchar(10)') as Component, 
        t.v.value('r[2]', 'varchar(10)') as Month1,
        t.v.value('r[3]', 'varchar(10)') as Month2,
        t.v.value('r[4]', 'varchar(10)') as Month3,
        t.v.value('r[5]', 'varchar(10)') as Month4,
        t.v.value('r[6]', 'varchar(10)') as Month5,
        t.v.value('r[7]', 'varchar(10)') as Month6,
        t.v.value('r[8]', 'varchar(10)') as Month7,
        t.v.value('r[9]', 'varchar(10)') as Month8,
        t.v.value('r[10]', 'varchar(10)') as Month9,
        t.v.value('r[11]', 'varchar(10)') as Month10,
        t.v.value('r[12]', 'varchar(10)') as Month11,
        t.v.value('r[3]', 'varchar(10)') as Month12
FROM @xml.nodes('/') as t(v)

SELECT *
FROM @TempTab 

输出:

idx Component   Month1  Month2  Month3  Month4  Month5  Month6  Month7  Month8  Month9  Month10 Month11 Month12
1   SINDA       --      --      --      --      --      --      --      --      30.00   --      --      --