views:

358

answers:

3

Hello, I have a table in the following format:

smallint, binary(10), smallint, smallint, varbinary(max)

I also have a csv file generated from the data in the table by one of our processes where binary data is exported as hexadecimal data.
I tried generating INSERT INTO statements which worked but was very slow..
I tired BULK INSERT but it is failing and i suspect it is because of the binary data.

Anyone know how can i use BULK INSERT (or something like that ) to insert big amount of data that includes binary data?

An example of the data:

1,0x00000000000000000BB8,0,142,0x434F4E534E554D424552202020202020040006000600202000000000202000000000000000008000422020202020202020202020202020202020000000000000000000000000000000000000202000000020000000000000000000000000000000000000000000000000000000000200020000000000000004000000E09304006B8016000600FFFFFFFF0B00010007004633303030002E81FFFF1C00FFFFFFFF04001E00

Thanks.

A: 

I tired BULK INSERT but it is failing and i suspect it is because of the binary data.

I suspect this is wrong. What error message did you receive?

Neil N
I'm getting: `Bulk load data conversion error (truncation) for row 1, column 2 (composite_key).`
Nimrod Shory
Truncation means your are trying to squeeze too many bytes into the column
Neil N
Thougt so.. But in a an insert statement it works.. What am I doing wrong? (I have added an example of a line i'm trying to insert..)
Nimrod Shory
Ya that is interesting, can you post both the full insert and bulk insert statements?
Neil N
+1  A: 

I bet the problem is that 0x434F4E... is being treated as the actual bytes to be inserted (0x3078343334463445) rather than as a hexadecimal expansion. ('0' = 0x30, 'x' = 0x78, '4' = 0x34 and so on.) The truncation would be occurring because hex has two characters per value, so it's trying to insert a string double the length of the one you want.

If you look into the options for BULK INSERT and find no way to tell it to interpret hex as binary, I recommend using SSIS for this. I have no actual experience bulk loading binary values from SSIS, but undoubtedly it can do it, and it will be fast.

I guess there's always the possibility of outputting the actual bytes of the binary values rather than a hexadecimal representation of them, but you're going to run into problems if you're using a delimiter, since the delimiter could be one of the bytes in a binary value. This is exactly the problem of mixing text and binary data. You might be able to do this by using fixed-length column imports, with the special extended syntax for BULK INSERT that defines the columns and their data types.

Emtucifor
+1 Thanks for the help
Nimrod Shory
A: 

When using a bulk insert with a data file and a format file generated from BCP it's working good. and its good enough for me.
Thanks for everyone who helped me out.

Nimrod Shory