Copying the Table and Data using SELECT INTO STATEMENT in SQL SERVER

Copying the Table and Data using SELECT INTO STATEMENT in SQL SERVER 

It creates a new table in the destination, so if any table having same name in destination, then drop and continue, otherwise it throws error.

 -- Created table with Primary Key
CREATE TABLE MyOrdersPrimary
(
    OrderId int PRIMARY KEY NOT NULL,
    ProductName varchar(20)
);

-- Inserted some records
insert into MyOrdersPrimary values (1,'Samsung')
insert into MyOrdersPrimary values (2,'Nokia')

select * from MyOrdersPrimary

-- COPY THE (TABLE + DATA).
Select * into MyOrdersPrimaryCopy from MyOrdersPrimary

Note: the above wont copy the primary key to the copy table if you use SELECT INTO STATEMENT, only it copies table and data.

To Overcome the ABOVE problem:

If you want the complete Table structure,

CREATE Table by using source table script  and then use  "INSERT INTO" to copy data.

Thanks :)

Ebook Download
View all
Learn
View all