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


    在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发表的日志。。 网转。
     
  • 相关阅读:
    Android中开发习惯
    Activity、Window和View三者间的关系有一定的见解
    Android底层有一定的认识,研究过相关的Android源码
    安卓工程的启动过程
    OAuth2认证有一定的了解
    屏幕适配经验
    NOIP模拟赛14
    NOIP模拟赛13
    NOIP模拟赛12
    NOIP模拟赛11
  • 原文地址:https://www.cnblogs.com/hhq365/p/2940285.html
Copyright © 2020-2023  润新知