BULK INSERT
database_name
Is the database name in which the specified table or view resides. If not specified, this is
the current database.
schema_name
Is the name of the table or view schema. schema_name is optional if the default schema
for the user performing the bulk-import operation is schema of the specified table or
view. If schema is not specified and the default schema of the user performing the bulk-
import operation is different from the specified table or view, SQL Server returns an error
message, and the bulk-import operation is canceled.
table_name
Is the name of the table or view to bulk import data into. Only views in which all
columns refer to the same base table can be used. For more information about the
restrictions for loading data into views.
Data_file
Is the full path of the data file that contains data to import into the specified table or view.
BULK INSERT can import data from a disk (including network, floppy disk, hard disk,
and so on).
data_file must specify a valid path from the server on which SQL Server is running. If
data_file is a remote file, specify the Universal Naming Convention (UNC) name. A
UNC name has the form \\Systemname\ShareName\Path\FileName.
For example, E:\\Sales\update.txt.
BATCHSIZE =batch_size
Specifies the number of rows in a batch. Each batch is copied to the server as one
transaction. If this fails, SQL Server commits or rolls back the transaction for every
batch. By default, all data in the specified data file is one batch.
ROWTERMINATOR ='row_terminator'
Specifies the row terminator to be used for char and widechar data files. The default row
terminator is \r\n (newline character).
KEEPIDENTITY
Specifies that identity value or values in the imported data file are to be used for the
identity column. If KEEPIDENTITY is not specified, the identity values for this column
are verified but not imported and SQL Server automatically assigns unique values based
on the seed and increment values specified during table creation. If the data file does not
contain values for the identity column in the table or view, use a format file to specify
that the identity column in the table or view is to be skipped when importing data; SQL
Server automatically assigns unique values for the column.
KEEPNULLS
Specifies that empty columns should retain a null value during the bulk-import operation,
instead of having any default values for the columns inserted.
FIRSTROW =2
Specifies the number of the first row to load. The default is the first row in the specified
data file. FIRSTROW is 1-based.
We can import the data from .txt ,.csv,.xls,.xlsx file
BULK INSERT JOHN.[dbo].[Samp1le_Data]
FROM 'D:\STC-File\Student_ExamData.txt'
WITH
(
FIRSTROW =2,
FIELDTERMINATOR ='|',
ROWTERMINATOR ='\n'
)
BULK INSERT JOHN.[dbo].[Samp1le_Data]
FROM 'D:\STC-File\Student_ExamData.xls'
WITH
(
FIRSTROW =2,
DATAFILETYPE = 'char',
FIELDTERMINATOR ='\t',
ROWTERMINATOR ='\n'
)
BULK COPY PROGRAM
The SQL Server bulk copy feature supports the transfer of large amounts of data into or out of a
SQL Server table or view. Data can also be transferred out by specifying a SELECT statement.
The data can be moved between SQL Server and an operating-system data file, such as an ASCII
file. The data file can have different formats; the format is defined to bulk copy in a format file.
Optionally, data can be loaded into program variables and transferred to SQL Server using bulk
copy functions and methods.
For a sample application that demonstrates this feature,
An application typically uses bulk copy in one of the following ways:
Bulk copy from a table, view, or the result set of a Transact-SQL statement into a data
file where the data is stored in the same format as the table or view.
This is called a native-mode data file.
Bulk copy from a table, view, or the result set of a Transact-SQL statement into a data
file where the data is stored in a format other than the one of the table or view.
In this case, a separate format file is created that defines the characteristics (data type,
position, length, terminator, and so on) of each column as it is stored in the data file. If all
columns are converted to character format, the resulting file is called a character-mode
data file.
Bulk copy from a data file into a table or view.
If needed, a format file is used to determine the layout of the data file.
Load data into program variables, then import the data into a table or view using the bulk
copy functions for bulk copying in a row at a time.
[-a packet_size]
[-b batch_size]
[-c]
[-d database_name]
[-F first_row]
[-i input_file]
[-k]
[-L last_row]
[-P password]
[-r row_term]
[-R]
[-S [server_name[\instance_name]]
[-T]
[-U login_id]
-V
/?
-V (80 | 90 | 100| 110)
Performs the bulk-copy operation using data types from an earlier version of SQL Server.
This option does not prompt for each field; it uses the default values.
80 = SQL Server 2000
90 = SQL Server 2005
100 = SQL Server 2008 and SQL Server 2008 R2
110 = SQL Server 2012
database_name
Is the name of the database in which the specified table or view resides. If not specified,
this is the default database for the user.
You can also explicitly specify the database name with d-.
-T
Specifies that the bcp utility connects to SQL Server with a trusted connection using
integrated security. The security credentials of the network user, login_id, and password
are not required. If –T is not specified, you need to specify –U and –P to successfully log
in.
-U login_id
Specifies the login ID used to connect to SQL Server.
-P password
Specifies the password for the login ID. If this option is not used, the bcp command
prompts for a password.
FIRE_TRIGGERS
Specified with the in argument, any insert triggers defined on the destination table will
run during the bulk-copy operation. If FIRE_TRIGGERS is not specified, no insert
triggers will run. FIRE_TRIGGERS is ignored for the out, queryout, and format
arguments.
-S server_name[ \instance_name]
Specifies the instance of SQL Server to which to connect. If no server is specified, the
bcp utility connects to the default instance of SQL Server on the local computer. This
option is required when a bcp command is run from a remote computer on the network or
a local named instance. To connect to the default instance of SQL Server on a server,
specify only server_name. To connect to a named instance of SQL Server, specify
server_name\instance_name.
To create the format files:
**************************
EXEC MASTER..XP_CMDSHELL'BCP john.[dbo].[Samp1le_Data] format nul -N -f D:\STC-File\
StudentExampdata.fmt -T –USA –PJOHN@123 -SJOHNSON-PC\NEWSQL2012'
EXEC MASTER..XP_CMDSHELL'BCP "SELECT * FROM JOHN.[dbo].[Samp1le_Data]" QUERYOUT "D:\STC-
File\Student_ExamData.txt" -T –USA –PJOHN@123 -c -SJOHNSON-PC\NEWSQL2012 -r\n -t"|"'
EXEC MASTER..XP_CMDSHELL'BCP "JOHN.[dbo].[Samp1le_Data]" in "D:\STC-File\
Student_ExamData.txt" -T –USA –PJOHN@123 -c -SJOHNSON-PC\NEWSQL2012 -r\n -t"|"'
EXEC MASTER..XP_CMDSHELL'BCP "SELECT * FROM JOHN.[dbo].[Samp1le_Data]" QUERYOUT "D:\STC-
File\Student_ExamData_PipeSep.xls" -T –USA –PJOHN@123 -c -SJOHNSON-PC\NEWSQL2012 -r\n -
t"|"'