如何从表中复制另一列的值

时间:2013-11-11 12:59:19

标签: sql sql-server

有一些值的表

role_number  action_number    status
   4               2            1
   4               5            0
   4               8            1
   4               7            0
   4               10           1
   4               3            0

现在想要新的role_number具有相同的action_number和status,如何插入?例如,它必须像:

  role_number  action_number    status
       4               2            1
       4               5            0
       4               8            1
       4               7            0
       4               10           1
       4               3            0
       5               2            1
       5               5            0
       5               8            1
       5               7            0
       5               10           1
       5               3            0

5 个答案:

答案 0 :(得分:1)

这是使用INSERT / SELECT的可能解决方案:

INSERT INTO YourTable(role_number, action_number, status)
SELECT @NewRoleNumber, action_number, status
FROM YourTable
WHERE RoleNumber = @RoleNumberToBeCopied

答案 1 :(得分:1)

INSERT INTO YourTable
    (role_number, action_number, status)
SELECT role_number + 1, action_number, status
FROM YourTable
WHERE role_number = 4

答案 2 :(得分:0)

INSERT INTO [tablename] (role_number, action_number, status)
SELECT 5, action_number, status FROM [tablename] where role_number = 4

答案 3 :(得分:0)

只需从同一张表中插入

INSERT INTO TableName
SELECT MAX(role_number)+1, action_number, status FROM TableName WHERE role_number = 4

答案 4 :(得分:0)

DECLARE @oldRollNo int = 4,
        @newRollNo int = 5;
INSERT INTO tablename (role_number, action_number, status)
SELECT @newRollNo, action_number, status FROM tablename where role_number = @oldRollNo