SQL Server BCP tool - sql-server

I'm trying to IMPORT data from EXPORTED Dat file from SQL Server in this way:
bcp "SELECT FieldName FROM [BaseName].[dbo].[TableName] where xxxxxx=16"
queryout Message_out.dat -n -Uusername -Sservername
When I try to import dat to sql server like this
bcp basename.dbo.tablename in "path\to\datfile.dat" -c -T
I get error:
Error = [Microsoft][SQL Server Native Client 10.0]Unexpected EOF
encountered in BCP data-file
regards, Grigor.

Try to explicitly indicate the field and row terminators for your file, for example, if your file is comma delimited and each row is in a new line:
bcp basename.dbo.tablename in "path\to\datfile.dat" -c -T -r\n -t,
And if if there are any other peculiarities in the format of your file, use the options to help bcp understand your file format using the options. A detailed documentation is available at msdn.
I see this question is old, but maybe it will help someone in the future...

Related

bcp export varbinary to flat file

I want to export varbinary with bcp to a flat file (csv).
It seems to work, but in front of the 0x is a strange enconding, I can not get rid of. Can someone explain what I'm doing wrong? Following the bcp-command I execute and the screenshot from notepad++
PS C:\Windows\system32> bcp "SELECT TOP 5 CONVERT(varchar(max), [Bild],1) AS SomeImageFieldAsHex FROM [SWTA_HH].[dbo].[Fenster]" queryout "c:\export.csv" -d "SWTA_HH" -T -n -r \n
Thanks Darius
As per the bcp Utility documentation, the -n switch is instructing BCP to output data in "native" format, which includes binary information to allow a receiving SQL Server to ingest the data again using the correct data types.
If you're wanting to output these varbinary values to a .CSV (comma-separated values) file then you'll probably want to use the -c switch instead, for character data, e.g.:
bcp "SELECT TOP 5 CONVERT(varchar(max), [Bild], 1) AS SomeImageFieldAsHex FROM [SWTA_HH].[dbo].[Fenster]" queryout "c:\export.csv" -d "SWTA_HH" -T -c -r \n

SQL Server OPENROWSET error reading bcp file

I'm trying to transfer table data from one SQL Server to another and wanting to use the bcp utility for it. This is purely to transfer data between two identical schemas, but I'm not able to use something like SSDT; I need something that can be scriptable and portable so it can be run by others with just SQL server and SSMS access.
I am generating a native output file and format file like so:
$> bcp database.TableName OUT c:\data\bcp\TableName.bcp -T -N -S SQLINSTANCE
$> bcp database.TableName format nul -f c:\data\bcp\TableName.fmt -T -N
Then in Management Studio I am trying to in turn read the files like this:
SELECT
*
FROM
OPENROWSET (BULK 'c:\data\bcp\TableName.bcp',
FORMATFILE = 'c:\data\bcp\TableName.fmt') AS t1
But am getting this error:
The bulk load failed. The column is too long in the data file for row 6, column 19. Verify that the field terminator and row terminator are specified correctly.
I have followed this process before successfully, and it works for other tables. But I'm running into issue with this table. The column mentioned is of datatype nvarchar(max). I can inspect what I think is the "problem" record in the source data and it's just a very long string but I don't see anything else special about it.
Is there something else I should be doing when generating the format file or what else am I missing?
If you are only exporting for the purpose of importing to another SQL Server, native format is the way to go. And is this case you don't need to use format files. Just do a native export and import.
Note you are specifying a capital -N and that's not native. Native is lower -n.
You should export using something like:
bcp database.Schema.TableName OUT c:\data\bcp\TableName.bcp -T -n -S SQLINSTANCE
Then on the importing side I sugest using BULK IMPORT, which don't need a format file for native at all:
BULK INSERT TargetDB.dbo.TargetTable
FROM 'c:\data\bcp\TableName.bcp'
WITH (DATAFILETYPE = 'native');
If you can't use BULK INSERT and must absolutely go for OPENROWSET, you need a format file. bcp can generate that for you, but again, lower case -n:
bcp database.Schema.TableName format nul -f c:\data\bcp\TableName.fmt -T -n -S SQLINSTANCE
Now your OPENROWSET should work.

SQL Server BLOB image column - extracting with BCP queryout - corrupted files AND bug

I need to export pdf and image files from a column in SQL Server 2012. Jonas's 3-step process in "How to export an image column to files in sql server" is the clearest instruction I've found. I did everything exactly as he stated. Two notes:
Where he says "your_db" in the BCP statement, you need database_name.schema_name.table_name.
No line breaks are allowed.
I was able to export files one at a time after this, but they were corrupt. The files were slightly smaller (by 1-15 KB) than the actual working PDFs that I can access through the UI.
This turned out to be a format issue - if you're not exporting XML files, you have to create a special format file. There's a great solution by Conor, in "SQL Server BCP export corrupted file" that tells how to create a format file and reference it in your BCP query. First I created the format file from the command line:
C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn>bcp CentricityPM.dbo.PatientProfileAttachment format nul -T -n -f C:\bcpdir\bcpfile.fmt
Then I edited and re-saved the format file as per Conor's post. I ran my BCP query:
EXEC master ..xp_cmdshell 'BCP "SELECT data FROM CentricityPM.dbo.PatientProfileAttachment WHERE PatientProfileid = ''11568'' AND type = ''pdf'' " queryout "C:\exportdir\testfile.pdf" -T -N'
The error:
Starting copy...
SQLState = S1000, NativeError = 0
Error = [Microsoft][SQL Server Native Client 11.0]Host-file columns may be skipped only when copying into the Server
SQLState = S1000, NativeError = 0
Error = [Microsoft][SQL Server Native Client 11.0]Unable to resolve column level collations
NULL
BCP copy out failed
Jon of All Trades noted in "BCP Error: columns may be skipped only when copying into the Server" that this is a Microsoft bug reported on 8/6/2010. He suggested creating a table with the right number of columns. I created a table with one column and one row of my data (?!) which Conor had actually referenced in his post but I didn't really get it until this point.
Please note, this is NOT useful to me, because I need not only the data, but a way to identify it (I have the name I want for each file stored in another column). But I gave it a try anyway - I re-ran the bcp format file:
C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn>bcp CentricityPM.dbo.PatientProfileAttachment format nul -T -n -f C:\bcpdir\bcpfile.fmt
Here's the format file it gave me:
11.0
1
1 SQLIMAGE 4 0 "" 1 data ""
And here are the edits I made - I changed the data type as TT suggested below, and changed the 4 to a 0:
11.0
1
1 SQLBINARY 0 0 "" 1 data ""
I ran my query:
EXEC master ..xp_cmdshell 'BCP "SELECT data FROM CentricityPM.dbo.TempImageFour" queryout "C:\exportdir\testfile.pdf" -T -fC "C:\bcpdir\bcpfile.fmt" '
It ran with no errors... but the file is still corrupted.
Can anyone see anything I've done wrong? Does anyone know a workaround for the one-column-only bug? Or if you know of a working tool that will do this for me, that'd be great too. I tried https://sqlblobextractor.codeplex.com/ early on, with no success.
You are using parameter -f "C:\bcpdir\bcpfile.fmt" but from my experience that should be -fC "C:\bcpdir\bcpfile.fmt". To be honest I don't remember anymore why... I once made something similar to export files (.zip) from database and my command has -fC parameter for the export file. I whish I could give you a proper explanation. Anyway, HTH.
Try the following command:
EXEC master..xp_cmdshell 'BCP "SELECT data FROM CentricityPM.dbo.TempImageFour" QUERYOUT "C:\exportdir\testfile.pdf" -T -fC "C:\bcpdir\bcpfile.fmt"'
An alternative is to specify the -C RAW option. This specifies that no conversion is done from one code page to another.
EXEC master..xp_cmdshell 'BCP "SELECT data FROM CentricityPM.dbo.TempImageFour" QUERYOUT "C:\exportdir\testfile.pdf" -T -f "C:\bcpdir\bcpfile.fmt" -C RAW'
Also, make sure that your format file has SQLBINARY as data type for your column.

bcp with format file using command dos in windows

I'am trying to import data into sql server table from a file using a format file.
In fact I have 2 databases: a production database and a local database
I want to insert some row of the table shipper of the production database in the local one. The table shipper don't have neither the same columns nor the same order of column in the 2 databases.
That's why I used a file format to do my bcp.
I generate file containing the rows I want to insert in my local database with the following commande
bcp "SELECT shipper_id,Shipper_name FROM ProductionDatabase.dbo.shipper where shipper_id >5" queryout shipper.txt -c -T
It works !!
I generate then the format file with the schema of my local table with the following commande
bcp LocalDatabase.dbo.shipper nul -T -n -f shipper-n.fmt
It works !!
Unfortunately when I tried to insert the file data in my local table
with the following commande:
bcp LocalDatabase.dbo.shipper in shipper.txt -T -f shipper-n.fmt
it generates the following error (translated from french)
Can anyone know what is the problem and how can I get arround it.
Thanks in advance
unexpected end of file encountered in the bcp data file
Your format file does not match the data. You are exporting using text using -c
bcp "SELECT shipper_id,Shipper_name FROM ProductionDatabase.dbo.shipper where shipper_id >5" queryout shipper.txt -c -T
But your format file is made for native (binary) data using -n
bcp LocalDatabase.dbo.shipper nul -T -n -f shipper-n.fmt
Either export both as native (my recommendation), or both as text. To prevent this error, export the data file and the format file at the same time, simply add -f shipper.fmt to your export
Text version:
bcp "SELECT shipper_id,Shipper_name FROM ProductionDatabase.dbo.shipper where shipper_id >5" queryout shipper.txt -c -T -f shipper.fmt
or
Native Version:
bcp "SELECT shipper_id,Shipper_name FROM ProductionDatabase.dbo.shipper where shipper_id >5" queryout shipper.txt -n -T -f shipper.fmt
PS. Since you can run into scenarios where your record or row delimiters exist in the data you should pick a character sequence that does not exist in your data as a separator for instance -t"\t|\t" (Tab-Pipe-Tab) for fields and -r"\t|\n" (Tab-Pipe-Newline) for rows. If you combine the format statement with the export the data and the format file will match and you have the freedom to change the separators on a single command line.
Specify separators after the -n or -c on the command line

BCP text file with special characters (SQL Server)

I'm stuck at a very simple bcp and am hoping someone here could tell what i am doing wrong...
I have a single column textfile contaning email addresses. I'm trying to BCP this to SQL server using "bcp" command. Below is the sample file and the command I'm using.
I have used bcp before with different sets of files, but never had an issue. Any idea why this file wouldn't load?
D.out
'a#a.com'
'b#b.com'
'c#c.com'
bcp command:
bcp temp_sf_email in D.out -SMyDBServer\INSTANCEA -T -w
Looks basic. But when run, doesnt do anything. The output I got is:
Starting Copy.....
0 Rows copied.
Thanks in advance for any suggestions..
Regards,
Simak
I would like to suggest this sintax for BCP command
bcp db_name.schema_name.table in "drive:\folder\file.ext" -SServer -T -c
example for your case:
bcp yourdb.dbo.temp_sf_email in "c:\folder\D.out" -SMyDBServer\INSTANCEA -T -c
use the flag -c that means -c character type instead -w
for more info about BCP click here
NOTE: I tested this example in SQL2005 and it work, btw you didnt specificy which SQL version was used
Hope it help

Resources