Monday, October 31, 2011
Copy table into new temporary table and put data back in original table
Sometimes you need to modify a table, like add a new column when there are constrains in place and you may be asked to drop the table before you can modify it. One solution is to store data in a temporary table, make the change and place the data back.
This example works when you have an identity column (autoId)
--- (1)Create new table (TempTable1) and insert into this table using SELECT INSERT
SELECT *
INTO TempTable1
FROM Table1
--- (2)Modify the Table1 --> add columns etc
--- (3)Copy the data from TempTable1 to the original Table1
--- It is important to list all the fields but the autoId (if there is one)
INSERT INTO Table1 (col1,col2,....colN)
SELECT col1,col2,....,colN
FROM TempTable1
Labels:
copy table,
SQL Server,
SQL Server Tip
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment