.SQL Server中 image类型数据的比较
生活随笔
收集整理的這篇文章主要介紹了
.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
那怎么樣對數據庫中的圖片做比較呢。
對于這種大型對象的處理,在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發表的日志。。 網轉。?
轉載于:https://www.cnblogs.com/hhq365/archive/2013/03/02/2940285.html
總結
以上是生活随笔為你收集整理的.SQL Server中 image类型数据的比较的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于重复接收NSNotification
- 下一篇: sizeof问题