2

In one of my solutions, i am using SQL Server's Change Tracking capabilities. To differentiate between changes made by the actual application and ones made by synchronizing applications, we are using Change Context. Now, this change context, as i know, is of type varbinary(128).

In the application logic, we create a byte array with some random bytes to be used as Change Context and when the synchronizing application makes any change, it uses this Change Context. Then, when we try to get the change set which were made by the actual application, with out any Change Context, we use the following code snippet in the query

SELECT .... from .... WHERE (SYS_CHANGE_CONTEXT != @context OR SYS_CHANGE_CONTEXT IS NULL)

Here, @context is command parameter that of type VARBINARY(128) which is added to the sqlcommand to get the change set. It would still get all the changes made regardless of the Change Context passed to the command.

Is there any special ways to compare VARBINARY values or send them as parameters??

Dipin Behl
  • 123
  • 1
  • 8

1 Answers1

2

Literally answering your question, I'm sure there are special ways to compare Varbinary's (especially large ones such as pre-computing a hash to use as a pre-compare), however the regular "=" operator works just fine:

declare @a varbinary(max) = 0x12345678;
declare @b varbinary(max) = 0x12345678;
declare @c varbinary(max) = 0x12345679;
declare @d varbinary(max) = null;
declare @e varbinary(max) = null;

select iif(@a = @b, 1, 0) as AreEqual -- 1
union all
select iif(@a = @c, 1, 0) as AreEqual -- 0
union all
select iif(@a = @d or @a is null and @d is null, 1, 0) as AreEqual -- 0
union all
select iif(@d = @e or @d is null and @e is null, 1, 0) as AreEqual -- 1

From C# Entity framework they appear as byte[]. I am not aware of any special requirements for passing as parameters besdies memory footprint. For larger ones (than your example) streams can be employed (e.g. 1, 2)

Community
  • 1
  • 1
crokusek
  • 5,345
  • 3
  • 43
  • 61