简体   繁体   English

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

[英]How to copy values from table for another column

have table with some values 有一些值的表

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

Now want new role_number with same action_number and status , how to insert it ? 现在想要具有相同action_number和status的新role_number,如何将其插入? for example it must be like : 例如,它必须像:

  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

Here it is a possible solution using INSERT/SELECT: 这是使用INSERT / SELECT的可能解决方案:

INSERT INTO YourTable(role_number, action_number, status)
SELECT @NewRoleNumber, action_number, status
FROM YourTable
WHERE RoleNumber = @RoleNumberToBeCopied
INSERT INTO YourTable
    (role_number, action_number, status)
SELECT role_number + 1, action_number, status
FROM YourTable
WHERE role_number = 4
INSERT INTO [tablename] (role_number, action_number, status)
SELECT 5, action_number, status FROM [tablename] where role_number = 4

Just insert from the same table 只需从同一张表插入

INSERT INTO TableName
SELECT MAX(role_number)+1, action_number, status FROM TableName WHERE role_number = 4
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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将一个表的两列的值复制到另一个表的一列中 - copy values of two column of a table into a column from another table 一条sql语句,如何将1列中的值复制到同一表中的另一列 - A sql statement, how to copy values in 1 column to another column in that same table 使用+列将表值复制到另一个表 - Copy table values to another table with + column 在 SQL 中,如何将一整列从一个表复制到另一个没有任何相互列或值的表? - how do i copy one whole column from a table to another without any mutual columns or values in SQL? 如何将列从一个表复制到 SQL 服务器中的另一个表 - How to copy column from one table into another table in SQL Server 如何在另一个表中的两个特定值之间插入缺失值,然后复制其他列的值 - How to insert missing values between two specific values from another table and then copy down the other column's values 如何将带有值的表从一个数据库复制到另一个数据库表? - How to copy the table with values from one database to another database table? 如何将与外键对应的值从另一个表复制到该表? - How to copy values corresponding to the foreign key from another table to this table? 将列值复制到表的另一部分 - copy column values to another part of the table 将所有值从一个表的列复制到另一表的特定列 - Copy all values from a column of one Table to a specific column of another Table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM