比如表aaa中有两个字段a1、a2都是image类型select * from aaa where a1=a2报错:服务器: 消息 306,级别 16,状态 1,行 1
不能比较或排序 text、ntext 和 image 数据类型,除非使用 IS NULL 或 LIKE 运算符。
是不是得用什么函数之类才能进行比较啊?

解决方案 »

  1.   


    select * from aaa where a1 like a2   ??
      

  2.   

    like可以啊if object_id('tablename') is not null drop table tablename
    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
      

  3.   

    oracle中处理这种大对象都是有专门得函数包处理的,sqlserver应该也有类似的啊,哪位老大有经验的介绍一下啊
      

  4.   

    如果datelength(a1) <= 8000 and datelength(a2) <= 8000,可以这样
    cast(a1 as varbinary) = cast(a2 as varbinary)
      

  5.   

    自定义函数:if object_id('tablename') is not null drop table tablename
    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
      

  6.   

    这样比较效率不行,最好用其他字段做比较或者对image做个hash还比较。
      

  7.   

    自己用c++之类的写个组件? 配置扩展存储过程,或直接采用操作COM的方式调用之?
      

  8.   

    已经解决了,特别感谢dulei115