SQL Server provides a variety of tools and methods for data export and import. Here, I share my practical experience (only involving database and Excel, database and text file, database and database export and import).
(1) Database and Excel
method 1:
Interface tool using database client (SSMS). Right-click to select the database to export data, select "Task" - "Export Data", as shown in Figure 1 below, and follow the wizard step by step. The import is the opposite. When importing, SQL Server will create a new table by default, the field name is also the same as the imported Excel title by default, and the field data type will be defaulted. Of course, it can be modified in the wizard. It should be noted that if the title is not English but Chinese, the default field name is also Chinese, which will bring trouble to the subsequent data update operation, so it is better to use a meaningful English field name. After importing the data, insert/update the data into the business table by executing the statement.
figure-1: Task - Export Data
Method 2:
Starting from SQL Server 2005, you can directly copy the results queried on SSMS, and then paste them into Excel. For a small amount of data, it is very fast and convenient. It should be noted that long numbers may become in the form of scientific notation. You can specify the format of the column as text on Excel in advance.
To import, ctrl + c to copy the data in Excel, then select the relevant table, edit the data, and paste the data directly. However, it is not recommended to paste directly into the business table (if the table is blank and has no data, and the field order corresponds, you can do this), but it is recommended to paste it into a new intermediate table first, and then insert/update the data through the statement. business table.
The export and import of this method is suitable for a small amount of data, such as records within 5000 lines. It is not recommended for more than 5000 lines. The speed is slow. If the data is too large, it will be successful.
(2) Databases and text files, databases and databases
Data migration or exporting and importing between databases is actually quite convenient. For example, after backing up the database, restore it on a new machine. But it should be noted that the backup of the version before SQL2008 cannot be directly restored on SQL2012 or above, but through the intermediate SQL2008 to make a transition, restore the old version of the database to SQL2008, then make a backup, and finally restore it on SQL2012 .
If the backup file of the new version (take SQL2012 as an example below) is restored to the old version (take SQL2008 as an example), it will be troublesome. Generally, it is not supported to restore the backup file of the new version in the old version. Only by writing a script, the data of the new version can be imported into the old version.
method 1:
The first recommendation is to use a "linked server" where the data does not fall to the ground. Use the SSMS of SQL2012, connect to the instances of SQL2012 and SQL2008 at the same time, and import the data of SQL2012 into SQL2008 by writing scripts. The two instances can be connected through a linked server. Below are the setup steps.
figure-2: New Linked Server
figure-3: Linked server and data source
figure-4: Authentication
figure-5: After the creation is successful, you can directly browse the directory of the linked server, or use the statement to query.
You can also use scripts to create linked servers.
--Create linked server EXEC sp_addlinkedserver @server = ' LINKED_SERVER_TEST2 ' , --Accessed server alias @srvproduct = '' , @provider = ' SQLOLEDB ' , @datasrc = ' 192.168.88.6,11433 ' --Data source GO --Create login and password EXEC sys.sp_addlinkedsrvlogin @rmtsrvname = ' LINKED_SERVER_TEST2 ' , --Accessed server alias @useself = ' false ' , @locallogin = NULL , @rmtuser = ' sa ' , --Datasource login @ rmtpassword = ' psd123456 ' -- data source login password GO --Set data can be accessed EXEC sys.sp_serveroption @server = ' LINKED_SERVER_TEST2 ' , @optname = ' data access ' , @optvalue = N ' true ' GO
code-1: Script to create linked server
After the creation is successful, you can directly query the data.
figure-6: Querying linked servers for data
All servers and related properties can be queried through the view sys.servers.
figure-7: query all linked servers
The specified linked server can be deleted on SSMS or by running the following script.
--Delete linked server and all login EXEC sys.sp_dropserver @server = ' LINKED_SERVER_TEST2 ' , @droplogins = ' droplogins ' GO
code-2: delete linked server and all logins
详细请参考:https://technet.microsoft.com/zh-cn/library/ff772782%28v=sql.105%29.aspx
方法2:
如果两个实例不能连接,只能在SQL2012上导出数据,再到SQL2008上导入。SQLServer提供生成包含数据的脚本工具,下图2。在第三步的“高级”选项里有一项“Types of data to scripts”有三个选择:Data only,Schema and data,Schema only,分别是只生成数据、生成表(对象)和数据,表(对象)。还有生成脚本的版本“Script for Server Version”,下图3。其他选项,按实际需要选择。
figure-8:任务——生成脚本
figure-9:生成脚本的高级选项
也可以使用存储过程生成包含数据的脚本。这里介绍一个别人已经做写好存储过程:sp_generate_inserts。运行之后,会按表每条记录生成一条insert的语句
![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
code-3:sp_generate_inserts脚本源代码
在我的实际使用中,只有两三个参数比较常用,分别是@table_name、@from和@owner,如果表的架构使用默认的dbo,则可以省略。以下是一个使用的例子:
figure-10:使用sp_generate_inserts的一个例子
其他参数的用法,这里就不一一解释了。我经常使用这个存储过程做一些简单而少量(如数万行记录以内)的数据导出导入,比前面介绍的方法方便快捷许多。但这个存储过程支持处理一般常用的数据类型,像XML这种类型则不支持。还有,如果生成的数据太多太大,SSMS返回数据会很慢,甚至SSMS会挂了,这时还是使用SSMS自带的导出脚本到文件稳妥些。如果使用生成的数据脚本文件很大,几百MB甚至上GB,在导入时,就不能直接使用SSMS直接打开来执行了。可以使用SQLCMD实用工具来在执行脚本。如下面的一个例子,在D盘下有一个脚本1.sql,内容为:
USE AdventureWorks2008R2 GO SELECT * FROM Person.CountryRegion; GO
code-4:SQLMCD的测试脚本
在运行下输入CMD,输入:
sqlcmd -S localhost -d AdventureWorks2008R2 -i D:\1.sql
code-5:SQLMCD的命令
回车执行后如下图,SQLCMD的详细用法,请参考:https://msdn.microsoft.com/zh-cn/library/ms180944.aspx
和 https://msdn.microsoft.com/zh-cn/library/ms162773%28v=sql.105%29.aspx
figure-11:SQLCMD的测试例子
方法3:
Import bulk data using BCP export. You can refer to my other blog " BCP export and import large-capacity data practice ".
The above methods are the data export and import tools that I often use in my daily work. Each method has its own advantages and different usage scenarios. Using different combinations of methods can save a lot of time and improve work efficiency. Hope it helps you. If you have better suggestions or methods please let me know!