Sunday, 3 April 2016

How to Copy a table data from one table to another new table in SQL server ?

To Copy the table data from one table to another new table (Without create statement) in SQL server
Use the following syntax

Syntax : 
SELECT * INTO TARGET_TABLE FROM [DATABASENAME].[SCHEMANAME].EXISTING TABLE NAME [WHERE CONDITION]
Example :

SELECT * INTO StudentDetail_Temp from Sample.dbo.StudentDetail where UID <= 100



StudentDetail_Form --> Refers to Target table

Sample --> Refers to Database name
dbo --> Refers to Schema Name
StudentDetail  --> Refers to Existing table name


After executing the above T-SQL statement,the new table StudentDetail_Temp  will be created with same table structure of StudentDetail table.
In above example we are copying the data where UID less than or equal to 100.
So the new table will contain only UID value less than or equal to 100.
Note : "Where" condition is not mandatory in select statement.

This way of creating a table will be very helpful in Production database to immediately take the backup of particular table before bulk update or Delete statement.

No comments:

Post a Comment