简体   繁体   中英

SQL Server 2005 bulk insert binary types

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM