mssql - copy data from one table to another existing table

Hello friends,

Today, there was a need to insert data from one table to another table. There are many ways to insert data from one to another. Sql server provides a functionality to copy data from one to another using SELECT clause also. I hope it may be helpful for you.

Syntax

insert into <table name>
	select <field list> from <table name from copy data>

You can insert selected field also.

insert into <table name> (field list)
	select <field list> from <table name from copy data>

Example

insert into table2
	select * from table1
	
	insert into table2 (no,name,city)
	select no,name,city from table1

Comments

sayed farhan

I have two table say table1 and table2 all the columns in both table are same but table2 has some extra column in which i want to insert a some default value and important thing is name of tables is generated runtime

February 20, 2013, 1:51 AM
Reply
Pritesh

Hi Sayed,

In that case instead of using * you can use list of column name (see second example in post). Since you have runtime table name, you can generate sql statement runtime, stored in variable and pass it for execution. I am not sure which language you are coding but seems pretty easy.

February 24, 2013, 9:18 PM
Reply
NOTE : Comments are moderated, and will not appear until the author has approved them.
Post a Comment
  1. Leave this field empty

Required Field