Insert data from one table to other table in SQL Server (INSERT INTO SELECT and SELECT INTO)

INSERT INTO SELECT  and SELECT INTO Statements

We can insert data from one table to other table using below two statements.
  1. INSERT INTO SELECT Statement: This Statement will work if the table is exists. we will insert data from one table to other table. For using statement table should  be created previously. Before going to run below script, Please create this tables.


    create table Employee(eno int, ename varchar(30))
    
    insert into Employee(eno, ename) select eno,ename from employeeMaster
    
    select * from Employee
    
    drop table employee
    


  2. SELECT INTO: We no need to create a table. Table will create automatically and send data from one table to other. 

    select eno, ename into Employee from employeemaster 
    
    select * from employee
    
    drop table employee
    
Output of the above statements:


Most of the times we will use this concept with SQL Temp Table in stored procedures and functions.

This is one of the good SQL Server Interview Questions.
Share this post :

Post a Comment

Please give your valuable feedback on this post. You can submit any ASP.NET article here. We will post that article in this website by your name.

 
Support : Ranga Rajesh Kumar
Copyright © 2012. ASP.NET Examples - All Rights Reserved
Site Designed by Ranga Rajesh Kumar