分享

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

 昵称10504424 2013-03-04
在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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多