Professional Documents
Culture Documents
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
Example
Collapse | Copy Code
We can also select only a few columns into the destination table like below:
Collapse | Copy Code
Example
Collapse | Copy Code
Example
Collapse | Copy Code
Use this to copy a table across two databases in the same SQL Server instance.
Collapse | Copy Code
Example
Collapse | Copy Code
Any one of the following methods can be employed to copy a table into a destination
database on a different SQL Server.
1.
2.
3.
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)
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
----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/
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>
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
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
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
Example
SELECT * FROM Customers
ORDER BY Country DESC;
Try it yourself
Example
SELECT * FROM Customers
ORDER BY Country,CustomerName;