GeoVIP
GeoVIP

Reputation: 1564

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 ? 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

Upvotes: 1

Views: 57

Answers (5)

Vincent James
Vincent James

Reputation: 1148

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

Upvotes: 1

somesh
somesh

Reputation: 3578

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

Upvotes: 0

bjnr
bjnr

Reputation: 3437

Here it is a possible solution using INSERT/SELECT:

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

Upvotes: 1

Tobberoth
Tobberoth

Reputation: 9527

Just insert from the same table

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

Upvotes: 0

Janne Matikainen
Janne Matikainen

Reputation: 5121

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

Upvotes: 0

Related Questions