Update varbinary(MAX) field in SQLServer 2012 Lost Last 4 bits

后端 未结 1 853
礼貌的吻别
礼貌的吻别 2021-01-21 20:12

Recently I would like to do some data patching, and try to update a column of type varbinary(MAX), the update value is like this:

0xFFD8F

相关标签:
1条回答
  • 2021-01-21 20:49

    It seems that the binary constant 0xFFD8F...6DC0676 that you used for update contains odd number of hex digits. And the SqlServer added half-byte at the beginning of the pattern so that it represent whole number of bytes.

    You can see the same effect running the following simple query:

    select 0x1, 0x104
    

    This will return 0x01 and 0x0104.

    The truncation may be due to some limitaions in SSMS, that can be observed in the following experiment:

    declare @b varbinary(max)
    set @b = 0x123456789ABCDEF0
    set @b = convert(varbinary(max), replicate(@b, 65536/datalength(@b)))
    select datalength(@b) DataLength, @b Data
    

    The results returned are 65536 and 0x123456789ABCDEF0...EF0123456789ABCD, however if in SSMS I copy Data column I'm getting pattern of 43677 characters length (this is without leading 0x), which is 21838.5 bytes effectively. So it seems you should not (if you do) rely on long binary data values obtained via copy/paste in SSMS.

    The reliable alternative can be using intermediate variable:

    declare @data varbinary(max)
    select @data = DataXXX from Table_XXX where ID = XXX
    update Table_YYY set DataYYY = @data where ID = YYY
    
    0 讨论(0)
提交回复
热议问题