Please disable your adblock and script blockers to view this page
 

SQL SERVER: INSERT INTO SELECT FROM Statement to copy data from one table to another

Introduction: In this article I am going to share how to copy/select data from all or selected columns of one table and insert into other existing table in SQL. 


Description: The INSERT INTO SELECT statement copies/selects data from one table (Source Table) and inserts it into other existing table (Destination Table). Existing data in destination table will remain unaffected. New rows will be appended to last of existing records.

 Implementation: Let’s create a sample table and perform the desired operation.

 --Create a table(Source)
GO
CREATE TABLE tbBooks
(
    BookId        INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
    BookName   VARCHAR(100),
    Author         VARCHAR(100),
    Publisher      VARCHAR(100),
    BookPrice     DECIMAL(10,2)
)

--Add some dummy data into the table
GO
INSERT INTO tbBooks VALUES
('Asp.Net','Ajay','Rozy Publication',1200),
('C#.Net','Sahil','Jai Publication',1000),
('VB.Net','Nancy','Rozy Publication',970),
('MVC','Sahil','Amar Publication',1480),
('JAVA','Supreet','Sam Publication',850),
('PHP','Parvesh','Maya Publication',800)

 --Check inserted data
SELECT * FROM tbBooks 

BookId
BookName
Author
Publisher
BookPrice
1
Asp.Net
Ajay
Rozy Publication
1200.00
2
C#.Net
Sahil
Jai Publication
1000.00
3
VB.Net
Nancy
Rozy Publication
970.00
4
MVC
Sahil
Amar Publication
1480.00
5
JAVA
Supreet
Sam Publication
850.00
6
PHP
Parvesh
Maya Publication
800.00


--Create another table(Destination)
GO
CREATE TABLE tbSelectedBooks
(
    BookId        INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
    BookName   VARCHAR(100),
    Author        VARCHAR(100),
    Publisher     VARCHAR(100),
    BookPrice    DECIMAL(10,2)
)

  
--Let’s Copy data except BookId from tbBooks table and insert into tbSelectedBooks table

 Copy data from one table and insert to other:

INSERT INTO tbSelectedBooks(BookName, Author, Publisher, BookPrice) SELECT BookName, Author, Publisher, BookPrice FROM  tbBooks;

Note: We can copy data from all or any number of columns from one table and insert into other table

--Check inserted data
SELECT * FROM tbSelectedBooks     

BookId
BookName
Author
Publisher
BookPrice
1
Asp.Net
Ajay
Rozy Publication
1200.00
2
C#.Net
Sahil
Jai Publication
1000.00
3
VB.Net
Nancy
Rozy Publication
970.00
4
MVC
Sahil
Amar Publication
1480.00
5
JAVA
Supreet
Sam Publication
850.00
6
PHP
Parvesh
Maya Publication
800.00

Copy data based on condition:

Suppose we want to copy and insert only those records from tbBooks table where Book price is greater than 1000 then the query will be as:

--Let's first truncate all existing data for clarity
TRUNCATE TABLE tbSelectedBooks

INSERT INTO tbSelectedBooks(BookName,Author,Publisher,BookPrice) SELECT BookName,Author,Publisher,BookPrice FROM  tbBooks WHERE BookPrice>1000;

--Check inserted data
SELECT * FROM tbSelectedBooks     

BookId
BookName
Author
Publisher
BookPrice
1
Asp.Net
Ajay
Rozy Publication
1200.00
2
MVC
Sahil
Amar Publication
1480.00

Now over to you:
A blog is nothing without reader's feedback and comments. So please provide your valuable feedback so that i can make this blog better and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linkedin and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates.
Previous
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily.. ConversionConversion EmoticonEmoticon