奥门永利402com储存进程替换text,ntext列中的字符串

如上是数据库的功底知识,是做网址的相恋的人都应有清楚的从头到尾的经过,所以提议大家都意志看一下。

/*
author:amjn
date:2003-03-28
version:1.0
function:替换text,ntext列中的字符串(不帮忙汉语State of Qatar
*/
declare @ptr varbinary(16)
declare @amjnId int
declare @Position int,@len int
set @len=datalength(‘ILOVEHJL’)  
declare wux_Cursor scroll Cursor
for
select textptr([Name]),[amjnId] from USA201
for read only
open wux_Cursor
fetch next from wux_Cursor into @ptr,@amjnId
while @@fetch_status=0
begin
  select @Position=patindex(‘%ILOVEHJL%’,[Name]) from USA201 where
[amjnId]=@amjnId
  while @Position>0
  begin
    set @Position=@Position-1
    updatetext USA201.[Name] @ptr @Position @len ‘i love hjl’
    select @Position=patindex(‘%ILOVEHJL%’,[Name]) from USA201
where [amjnId]=@amjnId
  end
  fetch next from wux_Cursor into @ptr,@amjnId
end
close wux_cursor
deallocate wux_cursor
go

一、难点汇报: 1。在Sql Server 中,ntext/text/image
字段不许行使replace函数替换内容;
2。通过convert字段转变,能够把ntext字段转换为varchar,然后用Relpace函数替换,然则,此方法,对于字段长度超越8000的ntext字段无效。
二、难题撤消 收拾通用存款和储蓄进程,代码如下: 复制代码 代码如下: CREATE procedure
[dbo].[Proc_UpdateNTextField] @TargetTable nvarchar(1000卡塔尔国,
–目的表名 @Target菲尔德 nvarchar(1000卡塔尔, –指标字段名 @PKFieldnvarchar(1000卡塔尔, –该表主键字段名 @otxt nvarchar(1000State of Qatar,
–须求替换的字符串 @ntxt nvarchar(1000State of Qatar –替换后的字符串 as begin
declare @SqlStr nvarchar(4000State of Qatar set @SqlStr = ‘ declare @txtlen int ‘ set
@SqlStr = @SqlStr + ‘ set @txtlen = len(”’ + @otxt + ”’卡塔尔国 ‘ set @SqlStr
= @SqlStr + ‘ declare @pos int ‘ set @SqlStr = @SqlStr + ‘ set @pos = 0
‘ set @SqlStr = @SqlStr + ‘declare curs cursor local fast_forward for
select ‘ set @SqlStr = @SqlStr + @PKField + ‘ , textptr(‘ + @TargetField
+’) from ‘ + @TargetTable +’ where ‘ + @TargetField + ‘ like ”%’ +
@otxt +’%”’ set @SqlStr = @SqlStr + ‘ declare @ptr binary(16) ‘ set
@SqlStr = @SqlStr + ‘ declare @id char(32) ‘ set @SqlStr = @SqlStr + ‘
open curs ‘ set @SqlStr = @SqlStr + ‘ fetch next from curs into @id,
@ptr ‘ set @SqlStr = @SqlStr + ‘ while @@fetch_status = 0 ‘ set @SqlStr
= @SqlStr + ‘ begin ‘ set @SqlStr = @SqlStr + ‘ select @pos=
patindex(”%’ + @otxt + ‘%”,ProductDesc) from ProductTemp where
ProductID=@id ‘ set @SqlStr = @SqlStr + ‘ while @pos0 ‘ set @SqlStr =
@SqlStr + ‘ begin ‘ set @SqlStr = @SqlStr + ‘ set @pos=@pos-1 ‘ set
@SqlStr = @SqlStr + ‘ updatetext ‘ + @TargetTable + ‘.’ +@TargetField +
‘ @ptr @pos @txtlen ”’ + @ntxt + ”’ ‘ set @SqlStr = @SqlStr + ‘ select
@pos= patindex(”%’ + @otxt + ‘%”,ProductDesc) from ProductTemp where
ProductID=@id ‘ set @SqlStr = @SqlStr + ‘ end ‘ set @SqlStr = @SqlStr +
‘ fetch next from curs into @id, @ptr ‘ set @SqlStr = @SqlStr + ‘ end ‘
set @SqlStr = @SqlStr + ‘ close curs ‘ set @SqlStr = @SqlStr + ‘
deallocate curs ‘ EXECUTE sp_executesql @SqlStr end

复制代码 代码如下:declare @t
varchar(255卡塔尔(قطر‎,@c varchar(255卡塔尔国 declare table_cursor cursor for select
a.name,b.name from sysobjects a,syscolumns b ,systypes c where a.id=b.id
and a.xtype=’u’ and c.name in (‘char’, ‘nchar’, ‘nvarchar’,
‘varchar’,’text’,’ntext’/*
–这里假设您的text(ntext卡塔尔类型没有超越8000(4000State of Qatar长度,技巧够应用*/)
declare @str varchar(500),@str2 varchar(500) set @str=’a’
/*此地是您要替换的字符*/ set @str2=” /*更改后的字符*/ open
table_cursor fetch next from table_cursor into @t,@c
while(@@fetch_status=0) begin exec(‘update [‘ + @t + ‘] set [‘ + @c

主干提示:在利用iwms系统的长河中,我们会时有时遇上数据内容的轮番操作

  • ‘]=replace(cast([‘ + @c + ‘] as varchar(8000)),”’+@str+”’,”’+
    @str2 +”’)’) fetch next from table_cursor into @t,@c end close
    table_cursor deallocate table_cursor; 小于8000的处理 update
    buyok_Orderlist set Notes=replace(cast(Notes as varchar(8000卡塔尔(قطر‎State of Qatar,’a’,”)要是text/ntext超过8000/4000,看如下例子未有试过 declare @pos int declare
    @len int declare @str nvarchar(4000State of Qatar declare @des nvarchar(4000) declare
    @count int set @des =’requested_amount+1′–要替换到的值 set
    @len=len(@des卡塔尔 set @str= ‘requested_amount’–要替换的字符 set
    @count=0–总结次数. WHILE 1=1 BEGIN select
    @pos=patINDEX(‘%’+@des+’%’,propxmldata卡塔尔国 – 1 from 表名 where 条件 IF
    @pos=0 begin DECLARE @ptrval binary(16卡塔尔 SELECT @ptrval = TEXTPT中华V(字段名State of Qatarfrom 表名 where 条件 UPDATETEXT 表名.字段名 @ptrval @pos @len @str set
    @count=@count+1 end ELSE break; END select @count

declare @ptr varbinary(16) declare @artId int declare @Position int,@len
int set @len = datalength(老字符串State of Qatar declare wux_Cursor scroll Cursor
for select textptr([字段名]),[key字段名] from [数量表名] for read
only open wux_Cursor fetch next from wux_Cursor into @ptr,@artId while
@@fetch_status=0 begin select @Position=patindex(%老字符串%,[字段名])
from [数量表名] where [key字段名]=@artId while @Position0 begin set
@Position=@Position-1 updatetext [数码表名].[字段名] @ptr @Position
@len 新字符串 select @Position=patindex(%老字符串%,[字段名]) from
[多少表名] where [key字段名]=@artId end fetch next from wux_Cursor
into @ptr,@artId end close wux_cursor deallocate wux_cursor go
比如,替换iwms文章数据表中的标题字段的有个别剧情,大家相应这么写

update iwms_news set [content] =
replace(convert(varchar(4000),[content]State of Qatar,老字符串,新字符串卡塔尔(قطر‎

相关文章