且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SQL Server Management Studio 2012 - 从/导出/导入数据

更新时间:2023-01-30 16:28:34

我不太清楚我是否理解你的要求(我不知道你是否需要将你的数据导出到excel或者你想做某种备份)。 >

为了从单个表导出数据,您可以使用 批量复制工具 ,它允许您从单个表导出数据并导出/导入到文件。您还可以使用自定义查询导出数据。



重要的是,这不会生成Excel文件,而是另一种格式。您可以使用它将数据从一个数据库移动到另一个数据库(在这两种情况下都必须是MS SQL)。



示例:
创建格式文件: p>


Bcp [TABLE_TO_EXPORT]格式[EXPORT_FILE]-n -f[FORMAT_FILE]-S [SERVER] -E -T -a从表中导出所有数据:


bcp [TABLE_TO_EXPORT] out[EXPORT_FILE]-f[FORMAT_FILE]-S [SERVER] -E -T -a 65535


导入以前导出的数据:


bcp [TABLE_TO_EXPORT]在[EXPORT_FILE]-f[FORMAT_FILE] - S [SERVER] -E -T -a 65535


我将输出从hte导出/导入操作重定向到日志文件(通过追加> mylogfile.log广告命令的结尾) - 这有助于您导出大量数据。


I have table with more than 3 000 000 rows. I have try to export the data from it manually and with SQL Server Management Studio Export data functionality to Excel but I have met several problems:

  • when create .txt file manually copying and pasting the data (this is several times, because if you copy all rows from the SQL Server Management Studio it throws out of memory error) I am not able to open it with any text editor and to copy the rows;

  • the Export data to Excel do not work, because Excel do not support so many rows

Finally, with the Export data functionality I have created a .sql file, but it is 1.5 GB, and I am not able to open it in SQL Server Management Studio again.

Is there a way to import it with the Import data functionality, or other more clever way to make a backup of the information of my table and then to import it again if I need it?

Thanks in advance.

I am not quite sure if I understand your requirements (I don't know if you need to export your data to excel or you want to make some kind of backup).

In order to export data from single tables, you could use Bulk Copy Tool which allows you to export data from single tables and exporting/Importing it to files. You can also use a custom Query to export the data.

It is important that this does not generate a Excel file, but another format. You could use this to move data from one database to another (must be MS SQL in both cases).

Examples: Create a format file:

Bcp [TABLE_TO_EXPORT] format "[EXPORT_FILE]" -n -f "[ FORMAT_FILE]" -S [SERVER] -E -T -a 65535

Export all Data from a table:

bcp [TABLE_TO_EXPORT] out "[EXPORT_FILE]" -f "[FORMAT_FILE]" -S [SERVER] -E -T -a 65535

Import the previously exported data:

bcp [TABLE_TO_EXPORT] in [EXPORT_FILE]" -f "[FORMAT_FILE] " -S [SERVER] -E -T -a 65535

I redirect the output from hte export/import operations to a logfile (by appending "> mylogfile.log" ad the end of the commands) - this helps if you are exporting a lot of data.