设为首页 加入收藏

TOP

SQL Server中image类型数据的比较
2014-11-24 03:02:02 来源: 作者: 【 】 浏览:2
Tags:SQL Server image 类型 数据 比较
SQL Server中image类型数据的比较
在SQL Server中如果你对text、ntext或者image数据类型的数据进行比较。将会提示:不能比较或排序 text、ntext 和 image 数据类型,除非使用 IS NULL 或 LIKE 运算符。不过image也是不支持like比较的。
www.2cto.com
那怎么样对 数据库中的图片做比较呢。
对于这种大型对象的处理,在 Oracle中有有专门的函数DBMS_LOB.COMPARE,而SQLSERVER中没有专门的处理函数,
只能通过使用substring函数一段一段的从image数据中截取放到varbinary类型数据,最长8060字节(8k),
www.2cto.com
然后再对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
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Sql建立自定义函数(函数和调用实.. 下一篇SQL中DATALENGTH 用法

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·【超详细】JDK 下载 (2025-12-24 18:19:32)
·Java_百度百科 (2025-12-24 18:19:29)
·简介 - Java教程 - (2025-12-24 18:19:27)
·C++ 语言社区-CSDN社 (2025-12-24 17:48:24)
·CSDN问答专区社区-CS (2025-12-24 17:48:22)