--自定义函数:
if object_id('tablename') is not null drop table tablename
create table tablename
(id int identity(1, 1),
a1 image,
a2 image)
go
insert into tablename(a1, a2) select 'aaa', 'aaa'
insert into tablename(a1, a2) select 'bbb', 'bba'
insert into tablename(a1, a2) select 'ccc', 'ccc'
insert into tablename(a1, a2) select 'ddd', 'ddc'
--------------------------------------------------------------------------------
if object_id('Compare_Image') is not null drop function Compare_Image
go
create function Compare_Image(@a1 image, @a2 image) returns int
as
begin
declare @n int, @i int, @j int
declare @b1 varbinary(8000), @b2 varbinary(8000)
set @n = 1
if datalength(@a1) <> datalength(@a2) --长度不同
set @n = 0
else
begin
set @i = 0
set @j = (datalength(@a1) - 1) / 8000 + 1
while @i <= @j
begin
set @b1 = substring(@a1, @i * 8000 + 1, case @i when @j then datalength(@a1) % 8000 else 8000 end)
set @b2 = substring(@a2, @i * 8000 + 1, case @i when @j then datalength(@a2) % 8000 else 8000 end)
if @b1 <> @b2
begin
set @n = 0
break
end
set @i = @i + 1
end
end
return(@n)
end
go
select * from tablename where dbo.Compare_Image(a1, a2) = 1
/*
id a1 a2
1 0x616161 0x616161
3 0x636363 0x636363
*/
drop function Compare_Image
--------------------------------------------------------------------------------
drop table tablename