You are on page 1of 11

How to Copy a SQL Server Table?

http://blog.sqlauthority.com/2008/02/06/sql-server-import-csv-file-into-sql-serverusing-bulk-insert-load-comma-delimited-file-into-sql-server/
http://www.w3schools.com/sql/sql_orderby.asp

Introduction
In this tip, I will show you the various ways of copying a SQL Server table. The first
method of copying is the simplest way to copy a table into another (new) table in the
same SQL Server database. You have to keep in mind that this way of copying does not
copy constraints and indexes. The following illustrates a code template and a sample
usage:
Collapse | Copy Code

select * into <destination table> from <source table>

Example
Collapse | Copy Code

Select * into employee_backup from employee

We can also select only a few columns into the destination table like below:
Collapse | Copy Code

select col1, col2, col3 into <destination table>


from <source table>

Example
Collapse | Copy Code

Select empId, empFirstName, empLastName, emgAge into employee_backup


from employee

Use this to copy only the structure of the source table.


Collapse | Copy Code

select * into <destination table> from <source table> where 1 = 2

Example
Collapse | Copy Code

select * into employee_backup from employee where 1=2

Use this to copy a table across two databases in the same SQL Server instance.
Collapse | Copy Code

select * into <destination database.dbo.destination table>


from <source database.dbo.source table>

Example
Collapse | Copy Code

select * into Mydatabase2.dbo.employee_backup


from mydatabase1.dbo.employee

Any one of the following methods can be employed to copy a table into a destination
database on a different SQL Server.
1.

Data Transformation Service (DTS) SQL Server 2000

2.

SQL Server Integration Service (SSIS) SQL Server 2005

3.

SQL Server Export Data task SQL Server 2000/2005

4.

Create a linked Server of the destination SQL Server on the source SQL Server
and then copy the table SQL Server 2000/ 2005

5.

We can also use sp_generate_inserts to generate data insertion scripts and then run
the insert scripts

6.

I almost forgot this you can open the source table , select the row(s), copy (ctrl
+ C) the row(s), open the destination table and then paste (ctrl + V) the row(s)

How to Quickly Create a Copy of a Table using Transact-SQL


The easiest way to create a copy of a table is to use a Transact-SQL command. Use
SELECT INTO to extract all the rows from an existing table into the new table. The new
table must not exist already. The following example will copy the Customers table under
the Sales schema to a new table called CurrCustomers under the BizDev schema:
SELECT * INTO BizDev.CurrCustomers FROM Sales.Customers

You can also create the new table from a specific subset of columns in the original table.
In this case, you specify the names of the columns to copy after the SELECT keyword.
Any columns not specified are excluded from the new table. The following example
copies specific columns to a new table:
SELECT CustName, Address, Telephone, Email INTO BizDev.CurrCustomers
FROM Sales.Customers

-- Method 1 : INSERT INTO SELECT


USE AdventureWorks2012
GO
----Create TestTable
CREATE TABLE TestTable (FirstName VARCHAR(100), LastName
VARCHAR(100))
----INSERT INTO TestTable using SELECT
INSERT INTO TestTable (FirstName, LastName)
SELECT FirstName, LastName
FROM Person.Person
WHERE EmailPromotion = 2
----Verify that Data in TestTable
SELECT FirstName, LastName
FROM TestTable
----Clean Up Database
DROP TABLE TestTable
GO
------------------------------------------------------------------------------------------------------------------ Method 2 : SELECT INTO
USE AdventureWorks2012
GO
----Create new table and insert into table using SELECT INSERT
SELECT FirstName, LastName
INTO TestTable
FROM Person.Person
WHERE EmailPromotion = 2
----Verify that Data in TestTable
SELECT FirstName, LastName
FROM TestTable

----Clean Up Database
DROP TABLE TestTable
GO
http://blog.sqlauthority.com/2008/02/06/sql-server-import-csv-file-into-sql-serverusing-bulk-insert-load-comma-delimited-file-into-sql-server/

SQL SERVER Import CSV File Into SQL Server Using


Bulk Insert Load Comma Delimited File Into SQL
Server
This is a very common request recently How to import CSV file into SQL Server? How to load CSV file into
SQL Server Database Table? How to load comma delimited file into SQL Server? Let us see the solution in
quick steps.
CSV stands for Comma Separated Values, sometimes also called Comma Delimited Values.
Create TestTable

USE TestData
GO
CREATE TABLE CSVTest
(ID INT,
FirstName VARCHAR(40),
LastName VARCHAR(40),
BirthDate SMALLDATETIME)
GO
Create CSV file in drive C: with name sweetest. text with the following content. The location of the file is
C:\csvtest.txt
1,James,Smith,19750101
2,Meggie,Smith,19790122
3,Robert,Smith,20071101

4,Alex,Smith,20040202

Now run following script to load all the data from CSV to database table. If there is any error in any row it will
be not inserted but other rows will be inserted.

BULK
INSERT CSVTest
FROM 'c:\csvtest.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
--Check the content of the table.
SELECT *
FROM CSVTest
GO
--Drop the table to clean up database.
DROP TABLE CSVTest
GO

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
?
1 insert into <table name>
2 select <field list> from <table name from copy data>

You can insert selected field also.


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

Example
?
1 insert into table2
2 select * from table1
3 insert into table2 (no,name,city)
4 select no,name,city from table1
5

You should do exactly as the error message says, use the SET IDENTITY_INSERT to
temporarily turn off the creation of identity values for your table, and provide each column
name in your INSERT and SELECT statements.
Here is the syntax for the SET IDENTITY_INSERT command:
SET IDENTITY_INSERT [ database_name . [ schema_name ] . ] table { ON | OFF }
Once you have completed inserting the data, re-run the SET command with OFF.
To copy data from one table to another

1. In Object Explorer, connect to an instance of Database Engine.


2. On the Standard bar, click New Query.
3. Copy and paste the following example into the query window and click Execute.
Copy

USE AdventureWorks2012;
GO
CREATE TABLE dbo.EmployeeSales
( BusinessEntityID
varchar(11) NOT NULL,
SalesYTD money NOT NULL
);
GO
INSERT INTO dbo.EmployeeSales
SELECT BusinessEntityID, SalesYTD
FROM Sales.SalesPerson;
GO

SQL INSERT INTO SELECT Statement

The ORDER BY keyword is used to sort the result-set.

The SQL ORDER BY Keyword


The ORDER BY keyword is used to sort the result-set by one or more columns.
The ORDER BY keyword sorts the records in ascending order by default. To sort the
records in a descending order, you can use the DESC keyword.

SQL ORDER BY Syntax


SELECT column_name,column_name
FROM table_name
ORDER BY column_name,column_name ASC|DESC;

Demo Database
In this tutorial we will use the well-known Northwind sample database.
Below is a selection from the "Customers" table:
CustomerI CustomerNam ContactNam
Address
D
e
e
Alfreds
1
Maria Anders Obere Str. 57
Futterkiste
Ana Trujillo
Avda. de la
2
Emparedados y Ana Trujillo
Constitucin
helados
2222
Antonio Moreno Antonio
Mataderos
3
Taquera
Moreno
2312
4
Around the Horn Thomas Hardy 120 Hanover

PostalCod
Country
e
German
Berlin 12209
y
City

Mxico
05021
D.F.
Mxico
05023
D.F.
Londo WA1 1DP

Mexico
Mexico
UK

Sq.
5

Berglunds
snabbkp

Christina
Berglund

Berguvsvge
Lule
n8

S-958 22

Sweden

ORDER BY Example
The following SQL statement selects all customers from the "Customers" table, sorted
by the "Country" column:

Example
SELECT * FROM Customers
ORDER BY Country;
Try it yourself

ORDER BY DESC Example


The following SQL statement selects all customers from the "Customers" table, sorted
DESCENDING by the "Country" column:

Example
SELECT * FROM Customers
ORDER BY Country DESC;
Try it yourself

ORDER BY Several Columns Example


The following SQL statement selects all customers from the "Customers" table, sorted
by the "Country" and the "CustomerName" column:

Example
SELECT * FROM Customers
ORDER BY Country,CustomerName;

You might also like