Friday, July 5, 2013

SQL SERVER - Insert from one table to another Table


In SQL Database, You can probably insert data from one table into another using two methods.
1. Insert Into - This is used when the table to which the data is going to be inserted is already present.
2. Select Into -  This is used when the table to which the data is going to be inserted is not present and is to be created on the go when inserting the data.  

Syntax for Insert Into

INSERT INTO NewEmployeeTable (EmpId)
SELECT EmpId
FROM OldEmployeeTable 
In the above syntax the table NewEmployeeTable is already there and we are inserting EmpId from the OldEmployeeTable into the exisiting NewEmployeeTable.

Syntax for Select Into

SELECT * INTO  NewTableName FROM ExistingTableName
In the above syntax it creates the new table and inserts the data from the existing old table.Thus the insert form one table to another table is done in Sql Server using Tsql. 
Check this post to read how to Insert into Table from Stored Procedure.

No comments:

Post a Comment