Saturday 6 June 2015

Shadow Copy In SQL Server Without Using Create Command

Shadow Copy In SQL Server Without Using Create Command

Various methods of copying a table with a new name using a select statement in SQL Server. It takes a lots of time to create new empty table and defining all field names again. Various methods are explained below.
Creating Table in SQL Server
create table StudentDetails
(
roll_num int,
F_Name varchar(15),
L_Name varchar(15),
City varchar(15)
)

Insert values in above table and table will look like below image


Method I
Select all columns into new table but this way of copying will not copy constraints and indexes. This technique will copy all the columns of the source table (StudentDetails) to the destination table (StudentDetailsCopy).


Method II
Select only the columns we want into the new table. Suppose we want to copy roll_num, F_Name, City columns from StudentDetails table into the StudentDetailsCopy1 table. Constraints and indexes will not be copied.


Method III
To copy the structure of the source table into another new table with new name. Constraints and indexes will not be copied.


Here, 1=2 will prevent the data from being copied from StudentDetail to the StudentDetailCopy2 table.




No comments:

Post a Comment