您的位置:首页 > 数据库

.SQL Server中 image类型数据的比较

2013-03-02 17:55 393 查看
在SQL Server中如果你对text、ntext或者image数据类型的数据进行比较。将会提示:不能比较或排序 text、ntext 和 image 数据类型,除非使用 IS NULL 或 LIKE 运算符。不过image也是不支持like比较的。
那怎么样对数据库中的图片做比较呢。
对于这种大型对象的处理,在Oracle中有有专门的函数DBMS_LOB.COMPARE,而SQLSERVER中没有专门的处理函数,
只能通过使用substring函数一段一段的从image数据中截取放到varbinary类型数据,最长8060字节(8k),
然后再对varbinary类型数据进行比较。以下是一个比较image的函数例子:

IF object_id('compare_image') IS NOT NULL DROP FUNCTION compare_image
GO
create function compare_image(@a1 image, @a2 image) returns int
-- if match, return 1
as
begin
declare @n int, @i int, @j int
declare @b1 varbinary(8000), @b2 varbinary(8000)
set @n = 1
if datalength(@a1) <> datalength(@a2) -- different length
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


内容来自本人QQ空间于2009-6-17 16:10发表的日志。。 网转。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: