HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMinor

How to troubleshoot "BCP copy in failed"

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
troubleshootbcpfailedhowcopy

Problem

I have a table that looks something like this in a SQL server database in Azure:

CREATE TABLE [dbo].[TableName](
[col01] [int] IDENTITY(1,1) NOT NULL,
[col02] [varchar](255) NOT NULL,
[col03] [varchar](255) NOT NULL,
[col04] [datetime] NULL,
[col05] [datetime] NULL,
[col06] [nvarchar](255) NULL,
[col07] [nvarchar](max) NULL,
[col08] [nvarchar](255) NULL,
[col09] [nvarchar](max) NULL,
[col10] [int] NULL,
[col11] [nvarchar](255) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO


I have a data file that looks a bit like this:

$ head datafile.csv 
2134024,SRC001,SE,NULL,NULL,NULL,NULL,NULL,NULL,1,SRC
2134025,SRC002,SE,NULL,NULL,NULL,NULL,NULL,NULL,2,SRC
2134026,SRC003,SE,NULL,NULL,NULL,NULL,NULL,NULL,3,SRC
2134027,SRC004,SE,NULL,NULL,NULL,NULL,NULL,NULL,4,SRC
2134028,SRC005,SE,NULL,NULL,NULL,NULL,NULL,NULL,5,SRC
2134029,SRC006,SE,NULL,NULL,NULL,NULL,NULL,NULL,6,SRC
2134030,SRC007,SE,NULL,NULL,NULL,NULL,NULL,NULL,7,SRC
2134031,SRC008,SE,NULL,NULL,NULL,NULL,NULL,NULL,8,SRC
2134032,SRC009,SE,NULL,NULL,NULL,NULL,NULL,NULL,9,SRC
2134033,SRC010,SE,NULL,NULL,NULL,NULL,NULL,NULL,10,SRC
$ wc -l 
713224
$


I try to import it using bcp running on my linux laptop:

$ bcp TableName in ./datafile.csv -S niceclouddbservername.database.windows.net -U myusername -dmydatabasename -c -t ','
Password: 

Starting copy... 

BCP copy in failed
$


Two questions, one is interesting right now:

  • Why doesn't this work?



  • How can I debug it?



What I have done so far:

  • Used duckduckgo (and google for good measure) to look for an explanation



  • Re-read the docs, found other examples, tried different variations



  • Tried with just the first 10 records



  • Verified (tried to at least) that the datatypes matches the input



  • Testing with a known wrong password to verify that it would produce a different error message



  • Consulted everything that seemed remotely related from the list of "Similar Questions"

Solution

Here is how I recommend doing it, which is tailored for character data:

  • generate format file



  • edit format file to match the input data structure



  • upload the data file using the format file generated at step 2



corresponding commands are then as follows:

  • run bcp [].. format nul -c -f


.fmt -t\ -S
-U


  • use any text editor if edition is needed



  • run bcp [].. in -f .fmt -t\ -b -m -e -S -U



Then, you can inspect ` to see how it went. Execution without any error should produce empty `.

Context

StackExchange Database Administrators Q#226897, answer score: 2

Revisions (0)

No revisions yet.