Join WhatsApp Channel Join Now
Join WhatsApp Group Join Now
Telegram Channel Join Now

Sunday 4 October 2015

How to copy entire table into another table in SQL Server

                When you receive a question from a interviewer as “write a SQL Query to copy a table into another table in SQL Server”, then you need to write a query. To write a query you have an idea on “Query to copy entire table into another table”.

                In the previous topics we are discussed about some Technical interview questions on SQL Server.

                Now we see how to write a SQL Query to copy entire table into another table in SQL Server

Freshers junction explains SQL Query to copy a table into another table in SQL Server with example for a better understand.

                For example you have table called “employee” with 4 columns and 6 records. You can see the table in the following image.
copy-table-into-another

So now you want to copy this (employee) entire table into another new table called “employeecopy”.
So SQL Query to copy a employee table into another employeecopy table in SQL Server

Insert into employeecopy select * from employee

Before writin the above query, you need to create employee copy as follows,

create table employeecopy (eno int,ename varchar(20),salary int,dno int)

If you want to copy a single column from old table into a new table then the SQL query is

INSERT INTO employeecopy (eno) SELECT eno FROM employee

In the above query eno column is copied into another table. (Here you can give any column which you want to copy)

If you want to copy a two columns from old table into a new table then the SQL query is

INSERT INTO employeecopy (eno, ename) SELECT eno, ename FROM employee

If you want to copy a three columns from old table into a new table then the SQL query is

INSERT INTO employeecopy (eno, ename, dno) SELECT eno, ename, dno FROM employee

In which situation this query is useful:

                If you have any sensitive data in table, then you can create a Copy of that table. Then you can perform a required operations on copy table. So the original table didn’t have any risk.

Tags: SQL Query to copy a table into another table in SQL Server, query to copy a entire table into another new table in sql server, query for copy a full table into another new table. SQL Query to copy a old table into new table.

0 comments:

 
close

Join Telegram Channel

Join FreshersJobsUpdates - Your Job Search End Here

Join Telegram Channel