Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy a table change values of 1 column and insert it in the same table

The title basically sums it pretty much all up, with what I'm trying to accomplish here.

Some more info, I got a table that has 4 columns. I need an exact copy of it but only need to change the values of one column.

Suppose that column is named customer number which is 123456 (the other values don't really matter).

How can I copy the entire table and change the customer number to 123457 and insert that copy back into the same table.

If everything went right, I should have twice as much (there is only one customer in the database) records as I had before where only the customer number has changed.

I'm using MSSQL2008 R2.

like image 502
Quoter Avatar asked Jan 04 '12 09:01

Quoter


People also ask

How do I copy data from one column to another column in the same table in Excel?

Move or copy just the contents of a cell You can also edit and select cell data in the formula bar. Select the row or column that you want to move or copy. In the cell, click where you want to paste the characters, or double-click another cell to move or copy the data. or press Ctrl+V.

How do I copy data into the same table based on existing data in SQL?

The SQL INSERT INTO SELECT Statement The INSERT INTO SELECT statement copies data from one table and inserts it into another table. The INSERT INTO SELECT statement requires that the data types in source and target tables match. Note: The existing records in the target table are unaffected.

How do I insert values from one column to another column in SQL?

INSERT INTO Syntax It is possible to write the INSERT INTO statement in two ways: 1. Specify both the column names and the values to be inserted: INSERT INTO table_name (column1, column2, column3, ...)

How can I UPDATE one column to another column in the same table in SQL?

UPDATE table SET col = new_value WHERE col = old_value AND other_col = some_other_value; UPDATE table SET col = new_value WHERE col = old_value OR other_col = some_other_value; As you can see, you can expand the WHERE clause as much as you'd like in order to filter down the rows for updating to what you need.


1 Answers

Try this

INSERT CustomerTable(customer, Field2, Field3, Field4)
SELECT 123457, Field2, Field3, Field4
from CustomerTable
WHERE customer = 123456
like image 79
Oleg Dok Avatar answered Sep 20 '22 03:09

Oleg Dok