比如表aaa中有两个字段a1、a2都是image类型select * from aaa where a1=a2报错:服务器: 消息 306,级别 16,状态 1,行 1
不能比较或排序 text、ntext 和 image 数据类型,除非使用 IS NULL 或 LIKE 运算符。
是不是得用什么函数之类才能进行比较啊?
不能比较或排序 text、ntext 和 image 数据类型,除非使用 IS NULL 或 LIKE 运算符。
是不是得用什么函数之类才能进行比较啊?
select * from aaa where a1 like a2 ??
create table tablename
(id int identity(1, 1),
aa text,
bb text)
go
insert into tablename(aa, bb) select 'aaa', 'aaa'
select 'ok' as aaa from tablename where aa like bb
/*
aaa
ok
*/
drop table tablename
cast(a1 as varbinary) = cast(a2 as varbinary)
create table tablename
(id int identity(1, 1),
a1 image,
a2 image)
go
insert into tablename(a1, a2) select 'aaa', 'aaa'
insert into tablename(a1, a2) select 'bbb', 'bba'
insert into tablename(a1, a2) select 'ccc', 'ccc'
insert into tablename(a1, a2) select 'ddd', 'ddc'
--------------------------------------------------------------------------------
if object_id('Compare_Image') is not null drop function Compare_Image
go
create function Compare_Image(@a1 image, @a2 image) returns int
as
begin
declare @n int, @i int, @j int
declare @b1 varbinary(8000), @b2 varbinary(8000)
set @n = 1
if datalength(@a1) <> datalength(@a2) --长度不同
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
select * from tablename where dbo.Compare_Image(a1, a2) = 1
/*
id a1 a2
1 0x616161 0x616161
3 0x636363 0x636363
*/
drop function Compare_Image
--------------------------------------------------------------------------------
drop table tablename