在SQL Server中怎样查询text或image类型数据的具体内容?

来源:百度知道 编辑:UC知道 时间:2024/06/27 07:22:09
特别是Image型图片信息。text数据稍长一些的一次显示完整。

1.在SQL Server中如果你对text、ntext或者image数据类型的数据进行比较。将会提示:不能比较或排序 text、ntext 和
image 数据类型,除非使用 IS NULL 或 LIKE 运算符。不过image也是不支持like比较的。

2.那怎么样对数据库中的图片做比较呢。

3.对于这种大型对象的处理,在Oracle中有有专门的函数DBMS_LOB.COMPARE,而SQLSERVER中没有专门的处理函数,
4.只能通过使用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)