Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server BCP export corrupted file?

i have a small problem with BCP functionality in SQL Server 2012. The things is: im loading .jpg image (167KB in size) using below command:

INSERT [tabela_testowa] ( Data ) SELECT * FROM OPENROWSET (BULK N'C:\foty\ch6_MagicShop.jpg', SINGLE_BLOB) a;

and then im trying to export it back to disk using:

BCP "SELECT data FROM tabela_testowa WHERE ID = 1" queryout "C:\test\file.jpg" -T -n -d test

File gets saved on disk no problem, size is also 167 KB but.. it cant be opened like the original copy. I dont know whatever some parameter is wrong in BCP export? Or maybe it gets corrupted at import stage? Anyone had similiar problems?

like image 928
user3494351 Avatar asked Dec 04 '14 18:12

user3494351


People also ask

How to export SQL Server data to CSV using BCP?

SQL Server Data Export to CSV using BCP. bcp is an SQL Server command line utility. There are many questions on the Internet about using bcp utility to export SQL Server data to CSV file. For example, the following command: bcp "SELECT * FROM dbo04.ExcelTest" queryout ExcelTest.csv -t, -c -S .

How do I use BCP in SQL Server?

The bcp utility is accessed by the bcp command. To use the bcp command to bulk import data, you must understand the schema of the table and the data types of its columns, unless you are using a pre-existing format file. The bcp utility can export data from a SQL Server table to a data file for use in other programs.

Why can't I import data from a BCP file?

If you use bcp to back up your data, create a format file to record the data format. bcp data files do not include any schema or format information, so if a table or view is dropped and you do not have a format file, you may be unable to import the data.

What is BCP Exe?

The bcp utility (Bcp.exe) is a command-line tool that uses the Bulk Copy Program (BCP) API. The bcp utility performs the following tasks: Bulk exports data from a SQL Server table into a data file. Bulk exports data from a query.


2 Answers

Thank god, thanks to @user_0 answer and @user3494351's cryptic answer and comment and this ancient forum post I finally figured this out after several hours of banging my head against the wall.

The issue is that BCP likes to add an extra 8 bytes to the file by default. This corrupts the file and makes it unable to be opened if you just use the native -n flag.

However, BCP allows you to specify a format file as output that can allow you to tell it not to add the extra 8 bytes. So I have a table I created (to be used in a cursor) in SQL Server that only has ONE ROW and ONE COLUMN with my binary data. Table must exist when you run the first command.

In command line first you need to do this:

bcp MyDatabase.MySchema.MyTempTable format nul -T -n -f formatfile.fmt

This creates formatfile.fmt in the directory you are in. I did on E:\ drive. Here's what it looks like:

10.0
1
1       SQLBINARY           8       0       ""   1     MyColumn             ""

That 8 right there is the variable that bcp says how many bytes to add to your file. It is the bastard that is corrupting your files. Change that sucker to a 0:

10.0
1
1       SQLBINARY           0       0       ""   1     MyColumn             ""

Now just run your BCP script, drop the -n flag and include the -f flag:

bcp "SELECT MyColumn FROM MyDatabase.MySchema.MyTempTable" queryout "E:\MyOutputpath" -T -f E:\formatfile.fmt
like image 96
Conor Avatar answered Nov 04 '22 01:11

Conor


BCP is adding informations to his file. Just few data, but you are not exporting just a jpg file.

You say 167 KB, but watch the real bytes, not the rounded dimension. There will be a difference.

You cannot export the image via BCP.

like image 37
user_0 Avatar answered Nov 03 '22 23:11

user_0