Use myDb go Create table myTable ( personId int identity primary key, firstName varchar(20), lastName varchar(20), reserved datetime default getdate(), submittedBy varchar(20) )Create base format file at command prompt (say we are working at c:\temp\sqlBulkLoad):
bcp MyDb..myTable format nul -S .\sqlexpress -T -c -f mytable.fmt -t "|" -r "|\n"This creates mytable.fmt file that looks like this:
8.0 5 1 SQLCHAR 0 12 "|" 0 personId "" 2 SQLCHAR 0 20 "|" 2 firstName SQL_Latin1_General_CP1_CI_AS 3 SQLCHAR 0 20 "|" 3 lastName SQL_Latin1_General_CP1_CI_AS 4 SQLCHAR 0 24 "|" 0 reserved "" 5 SQLCHAR 0 20 "|\r\n" 5 submittedBy SQL_Latin1_General_CP1_CI_ASHere is our test text file (c:\temp\SQLBulkLoad\myFile.txt):
first|last|by|
cetin|basoz|cetin|
john|doe|cetin|
mike|cole|cetin|
Now, in text file we only have 3 fields ( for this we simply remove the columns to be skipped and fixup the column orders in the leftmost position - this is one way, there are other ways)8.0 3 1 SQLCHAR 0 20 "|" 2 firstName SQL_Latin1_General_CP1_CI_AS 2 SQLCHAR 0 20 "|" 3 lastName SQL_Latin1_General_CP1_CI_AS 3 SQLCHAR 0 20 "|\r\n" 5 submittedBy SQL_Latin1_General_CP1_CI_ASWe are ready to bulk insert.
BULK INSERT MyTable FROM 'c:\temp\SQLBulkLoad\MyFile.txt' WITH ( FormatFile = 'c:\temp\SQLBulkLoad\MyTable.fmt', firstrow=2 )Cetin