请教达人,数据库中text数据类型中记录的内容比较多,其中内容中含有test字符,我想把test字符改为测试,在查询分析器中使用
update table set content=replace(cast(content as varchr(8000)),'test','测试')
出现如下提示
服务器: 消息 7102,级别 20,状态 1,行 1
SQL Server 内部错误。文本管理器无法继续执行当前语句。连接中断若在企业管理器中将text类型改为varchar类型,长度为8000,提示如下:
'table' 表
- 不能修改表。  
ODBC 错误: [Microsoft][ODBC SQL Server Driver][SQL Server]无法创建大小为 8150 的行,该值大于允许的最大值 8060。
[Microsoft][ODBC SQL Server Driver][SQL Server]语句已终止。请问怎么回事呢?应该怎么做才能替换字符??(整个表中有3000多条记录),3Q!!

解决方案 »

  1.   

    sql2000 不支持对 text 字段的直接操作,你这个情况,需要自己编写程序,进行表例遍处理
      

  2.   

    你出现的错误,是因为你的记录里面,有超过 8000 的,超过了 varchar(8000) 的范围
      

  3.   

    参考:--更新text字段的值
    create table PE_Soft(SoftIntro text,ChannelID integer) 
    insert into PE_Soft 
    select  'aaa ',1004 
    union all 
    select  'bbb ',1003 
    union all 
    select  'ccc ',1002 declare @ptr binary(16) 
    select @ptr=textptr(SoftIntro) from PE_Soft where ChannelID=1002 
    updatetext PE_Soft.SoftIntro @ptr null 0  'testing ' 
    select * from PE_Soft drop table PE_Soft
      

  4.   

    两个支持text字段内文字替换的存储过程
    csdn realgz
    (http://topic.csdn.net/u/20080505/20/d2dffbbe-6d6b-41cf-b29a-41149540eafa.html?seed=562172452)if object_id('sp_replaceTextWithMultiColPk') is not null 
    drop  proc sp_replaceTextWithMultiColPk 
    go create procedure sp_replaceTextWithMultiColPk 
    @tableName sysname,@colName sysname,@oldStr nvarchar(512),@newStr nvarchar(512),@whereStr nvarchar(200)='' 
    /* 
    为一个表内的text做统一替换的存储过程 
    缺点:慢,而且事实上还没办法支持任意表,而且代码看起来难受 
    因为 varchar的长度有限,当然还可以扩展,不过在sql 2005 里已经没这个问题了,懒得再写。   
    realgz 2008-05-05 */ 
    as 
    begin 
    set nocount on 
    declare @cursor nvarchar(4000),@fetch nvarchar(4000),@insert nvarchar(4000),@where nvarchar(4000) 
    declare @replaceExec nvarchar(4000),@checkExec nvarchar(4000) 
    declare @rpPtr varbinary(16),@rpPostion int,@rpLen int 
    if object_id('tempdb..#key') is not null 
    drop table #key --获得主键列表 
    select   sc.name keyName  into #key  from  
    sysobjects so  
    join  sysindexes idx on so.parent_obj=idx.id  
    join sysindexkeys idk on so.parent_obj=idk.id and idx.indid = idk.indid  
    join syscolumns sc on so.parent_obj=sc.id and idk.colid=sc.colid  
    where so.xtype='PK' and so.parent_obj =object_id(@tableName) and idx.status & 0x800>0 
      
      if @@rowcount  < 1 
    begin 
    raiserror ('表不符合要求或者没有这个表',12,1) 
    return 
    end 
    --增加键值列 
    alter table #key add keyValue sql_variant 
    alter table #key add primary key(keyName) --替换的长度 
    select @rpLen=len(@oldStr),@oldStr='%'+@oldStr+'%' --游标声明语句 
    select @cursor ='declare #c cursor static  for   select textptr('+@colname+'),PATINDEX ('''+replace(@oldStr,'''','''''')+''','+@colname+')-1' 
    select @cursor = @cursor +',['+keyName+']'  from #key   select @cursor = @cursor +' from ' +@tablename+' '+@whereStr   --为游标的提取生成语句 
    select @fetch='',@insert='',@where='' 
    select @fetch = @fetch +'declare @'+keyName +' sql_variant '+char(13) from #key 
    select @fetch = @fetch +'fetch next  from #c into @rpPtr,@rpPostion' 
    select @fetch = @fetch +',@'+keyName +char(13)  from #key  --把游标提取出来的值放到临时表的语句 
    select @insert = @insert +' insert into #key(keyName,keyValue ) ' 
    select @insert = @insert + ' select '''+replace(keyName,'''','''''')+''',@'+keyName+' union all'  +char(13)  from #key  
    select @insert = left(@insert,len(@insert)-10)   
    select @fetch=@fetch +@insert   
    --每次获得游标行对应text指针的语句 
    select @where =' where 1=1' 
    select @where =@where +' and ['+keyName+']= (select keyValue from #key where keyName = '''+replace(keyName,'''','''''')+''')' from #key 
      
    --改写的sql 
    select @replaceExec =  'updatetext '+@tablename+'.'+@colname+' @rpPtr @rpPostion  @rpLen @NewStr' --检查是否存在目标的sql 
    select @checkExec =' select @rpPostion = PATINDEX ('''+replace(@oldStr,'''','''''')+''','+@colname+')-1 from ' +@tablename+@where   --声明游标 
    exec sp_executesql @cursor  
    truncate table #key  
    open #c  --提取第一行 
    exec sp_executesql @fetch,N'@rpPtr varbinary(16) output,@rpPostion int  output',@rpPtr output,@rpPostion  output 
    --print @fetch 
      
    while @@fetch_status=0 
    begin 
      
    --只要@rpPostion>0 证明还需要替换  
      while @rpPostion>0 
      begin 
    --替换  
      
    exec sp_executesql @replaceExec,N' @rpPtr varbinary(16),@rpPostion int,@rpLen int,@newStr nvarchar(512)',@rpPtr,@rpPostion,@rpLen,@NewStr --重新判断是否需要替换 
    exec sp_executesql @checkExec,N'@rpPostion int output',@rpPostion output 
      end 
    truncate table #key  --提取下一行 
    exec sp_executesql @fetch,N'@rpPtr varbinary(16) output,@rpPostion int output',@rpPtr output,@rpPostion  output 
    end 
    close #c 
    deallocate #c 
    if object_id('tempdb..#key') is not null 
    drop table #key 
    set nocount off  
    end go