在同一个表中复制表数据

时间:2016-10-14 18:57:30

标签: sql-server

这有点难以解释,但我正在复制(复制)与id相关的数据。我想将数据序列从表1复制到表2.我可以手动执行此操作,但我移动了100个不同的ID,我必须更新数据。有没有办法可以更快地复制数据。我只需改变id就应该是相同的。

示例:

表1中的数据1

        +-------------+---------------+----------+----------------------------------+
        + item_number +  order_number +    id    +            text                  +
        +-------------+---------------+----------+----------------------------------+
        +     1       +      1        +   285    +          Question 1              +
        +-------------+---------------+----------+----------------------------------+
        +     1       +      2        +   285    +          Question 2              +
        +-------------+---------------+----------+----------------------------------+
        +     1       +      1        +   285    +          Question 3              +
        +-------------+---------------+----------+----------------------------------+
        +     1       +      1        +   285    +          Question 4              +
        +-------------+---------------+----------+----------------------------------+
        +     2       +      1        +   285    +          Question 5              +
        +-------------+---------------+----------+----------------------------------+
        +     2       +      1        +   285    +          Question 6              +
        +-------------+---------------+----------+----------------------------------+
        +     3       +      1        +   285    +          Question 7              + 
        +-------------+---------------+----------+----------------------------------+
        +     4       +      1        +   285    +          Question 8              +
        +-------------+---------------+----------+----------------------------------+
        +     4       +      1        +   285    +          Question 9              +
        +-------------+---------------+----------+----------------------------------+

表1中的数据2

    +-------------+---------------+----------+----------------------------------+
    + item_number +  order_number +    id    +            text                  +
    +-------------+---------------+----------+----------------------------------+
    +     1       +      1        +   356    +          Question 1              +
    +-------------+---------------+----------+----------------------------------+
    +     1       +      2        +   356    +          Question 2              +
    +-------------+---------------+----------+----------------------------------+
    +     1       +      1        +   356    +          Question 3              +
    +-------------+---------------+----------+----------------------------------+
    +     1       +      1        +   356    +          Question 4              +
    +-------------+---------------+----------+----------------------------------+
    +     2       +      1        +   356    +          Question 5              +
    +-------------+---------------+----------+----------------------------------+
    +     2       +      1        +   356    +          Question 6              +
    +-------------+---------------+----------+----------------------------------+
    +     2       +      1        +   356    +          Question 7              + 
    +-------------+---------------+----------+----------------------------------+
    +     3       +      1        +   356    +          Question 8              +
    +-------------+---------------+----------+----------------------------------+

仅供参考:我只是拥有相同表格的复制数据。意味着它类似于Update语句而不是Insert Statement

2 个答案:

答案 0 :(得分:0)

此声明是否适合您?

INSERT INTO table2
 SELECT * FROM table1;

答案 1 :(得分:0)

听起来您想要在ID字段上使用条件更新来复制数据。您可以使用CASE

执行此操作
SELECT 
item_number,
order_number,
case
  when id = 285 then 356
  --add other id changes
end as [id],
[text]
INTO Table2
FROM Table1