SQL Server big table data export and import command BCP

SQL Server backup and restore commands Sqlcmd, osql, iSql use: http://lzh166.iteye.com/blog/1123995
SQL Server BCP use summary: http://www.cnblogs.com/qanholas/archive/2011/07/05/ The above article on 2098616.html
     has already used related commands. Our article here mainly solves the situation that the number of table records is relatively large, such as 3 million records, the generated SQL script file size is 1.6G, and the file cannot be executed by SQL Server Management Studio. Big, use navicat to run the script out of memery, edit with editPlus, file too large, edit with UltrEdit, the speed is slow, want to use SQLDumpSplitter to split Sql files, invalid, really stupid idea. So just use the command.
1. SQLCMD
SQLCMD allows running scripts through the command line prompt in the Windows command window. The
syntax is as follows:
C:\Users\donald>sqlcmd -?
Microsoft (R) SQL Server Command Line Tools
Version 10.50.1600.1 NT x64
Copyright (c) Microsoft Corporation. all rights reserved.

Usage: Sqlcmd [-U login ID] [-P password]
  [-S server] [-H hostname] [-E trusted connection]
  [-N encrypted connection][-C trust server certificate]
  [-d use database name] [-l login timeout value] [-t query timeout value]
  [-h header] [-s column separator] [-w screen width]
  [-a packet size] [-e echo input] [-I allow quoted identifiers]
  [-c command end] [-L[c] list servers [clear output]]
  [-q "command line query"] [-Q "command line query" and exit]
  [-m errorlevel] [-V severity] [-W remove trailing spaces]
  [-u unicode output] [-r[0|1] message to stderr]
  [-i input file] [-o output file] [-z new password]
  [-f <codepage> | i:<codepage>[,o:<codepage>]] [-Z New password and exit]
  [-k[1|2] delete [replace] control characters]
  [-y variable length type display width]
  [-Y fixed length type display width]
  [-p[1] print statistics [colon format]]
  [-R use client locale]
  [-b abort batch on error]
  [-v variable="value"...] [-A dedicated admin connection]
  [-X[1] disable commands, startup scripts, environment variables [and exit]]
  [-x disable variable substitution]
  [-? show syntax summary]

C:\Users\donald>

Example:
execute sql script (import data):
sqlcmd  -S  localhost -U sa -P 123456   -i "E:\user.sql"

The import shows an error in the sql script file, grandma's can't.

2.OSQL
The following is the parameter description of the OSQL command:
C:\Users\donald>osql -?
Microsoft (R) SQL Server Command Line Tools
Version 10.50.1600.1 NT x64
Copyright (c) Microsoft Corporation. all rights reserved.

Note: osql does not support all features of SQL Server 2008 R2.
Please use sqlcmd. For more information, see SQL Server Books Online.
Usage: osql [-U login ID] [-P password]
  [-S server] [-H hostname] [-E trusted connection]
  [-d use database name] [-l login timeout value] [-t query timeout value]
  [-h header] [-s column separator] [-w column width]
  [-a packet size] [-e echo input] [-I allow quoted identifiers]
  [-L list servers] [-c end of command] [-D ODBC DSN name]
  [-q "command line query"] [-Q "command line query" and exit]
  [-n remove numbering] [-m error level]
  [-r message to stderr] [-V severity]
  [-i input file] [-o output file]
  [-p print statistics] [-b abort batch on error]
  [-X[1] disable command, [exit with warning]]
  [-O use legacy ISQL behavior to disable the following]
      <EOF> batch
      Automatically adjust console width
      wide message
      Default error levels are -1 and 1
  [-? show syntax summary]

example:
osql -E  -i "E:\user.sql"

The import time is too long, the smaller table can be imported successfully, and the following error is encountered during the execution process: The
object name 'dbo.test' is invalid: http://blog.csdn.net/zhangfy1015/article/details/51690316

3. BCP does not use the import and export commands of formatted files for processing large table data

C:\Users\donald>bcp -?
usage: bcp [[database_name.]owner.]table_name[:slice_number] {in | out} datafile
        [-m maxerrors] [-f formatfile] [-e errfile]
        [-F firstrow] [-L lastrow] [-b batchsize]
        [-n] [-c] [-t field_terminator] [-r row_terminator]
        [-U username] [-P password] [-I interfaces_file] [-S server]
        [-a display_charset] [-q datafile_charset] [-z language] [-v]
        [-A packet size] [-J client character set]
        [-T text or image size] [-E] [-g id_start_value] [-N] [-X]
        [-M LabelName LabelValue] [-labeled]
        [-K keytab_file] [-R remote_server_principal]
        [-V [security_options]] [-Z security_mechanism] [-Q]

C:\Users\donald>

Command example:
--export
bcp test.dbo.user out  E:\user.txt -c -T   

--import
bcp test.dbo.user  in E:\user.txt -c -T


Actual combat:
--Export data
(c) 2016 Microsoft Corporation. all rights reserved.

C:\Users\donald>bcp test.dbo.user out  E:\user.txt -c -T


Copying started...

2648040 rows copied.
Network packet size (bytes): 4096
Total clock time (ms): 25643 Average: (105921.56 lines per second.)
The generated txt file is only about 400M.

--Import Data
C:\Users\donald>bcp test.dbo.user  in E:\user.txt -c -T


Starting replication...
..1000
rows have been sent to SQL Server. Total sent: 2643000
1000 rows have been sent to SQL Server. Total sent: 2644000
1000 rows have been sent to SQL Server. Total sent: 2645000
SQLState = 22003, NativeError = 0
...
Error = [Microsoft][SQL Server Native Client 10.0]Value out of range 1000 rows
have been sent to SQL Server. Total sent: 2646000
1000 rows have been sent to SQL Server. Total sent: 2647000
SQLState = 22001, NativeError = 0
Error = [Microsoft][SQL Server Native Client 10.0]String data, right

truncation 2648040 rows copied.
Network Packet Size (Bytes): 4096
Total Clock Time (ms): 52196 Average: (50738.46 lines per second.)

C:\Users\donald>
Encountered the following error
CTLIB Message: - L6/O8/S5/N3 /5/0:
ct_connect(): directory service layer: internal directory control layer error: Requested server name not found.
Establishing connection failed.
Solution:
http://blog.sina.com.cn/s/blog_5ceb51480101gs4j.html
---Analyze the reason
I The Sybase database was installed in the system before, and the SQLSERVER database was installed after Sybase,
which caused the environment variable corresponding to SQLSERVER to be after Sybase. When executing the BCP command, the
first BCP found by the system was Sybase's BCP, and Not SQLSERVER's. Using Sybase's BCP to guide data to SQLSERVER will of course have problems.
----Solution
In the PATH environment variable, all the environment variables related to SQL Server are mentioned before Sybase, so that's it.
It seems that the environment variables of windows also have priorities, which should be paid attention to in subsequent applications.
Summary:
BCP commands are used for importing and exporting large table data, instead of importing and exporting data using formatted files;
OSQL can mainly handle data of the type of SQL script files. There may be data loss in bcp. Each table I tested lost 1 record. Is it because I made a mistake?

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326321312&siteId=291194637