SQL Server BCP export corrupted file?

前端 未结 3 613
温柔的废话
温柔的废话 2020-11-28 15:33

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] (

相关标签:
3条回答
  • 2020-11-28 15:49

    OK so i solved the issue. Format file has to be added using -f and path to the file. It can be create by running bcp without any format and order it to save format file to disk. Then we can use this format file so its no longer needed to answer those questions, and file itself has no additional data and can be opened without problems

    0 讨论(0)
  • 2020-11-28 15:59

    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
    
    0 讨论(0)
  • 2020-11-28 16:00

    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.

    0 讨论(0)
提交回复
热议问题