MSSQL Server数据库内容替换方法

update [swf_Upload] set [Dir] =
replace([Dir],200901/14,200901/15)update [swf_Content] set
[Description] =replace([Description],200901/14,200901/15)update
[swf_Content_01] set [content] = replace(convert(varchar(4000),
[content]),200901/14,200901/15)

/*
author:amjn
date:2003-03-28
version:1.0
function:替换text,ntext列中的字符串(不帮助中文卡塔尔国
*/
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(1000State of Qatar,
–指标表名 @TargetField nvarchar(1000卡塔尔, –指标字段名 @PKFieldnvarchar(1000卡塔尔, –该表主键字段名 @otxt nvarchar(1000卡塔尔(قطر‎,
–需求替换的字符串 @ntxt nvarchar(1000卡塔尔 –替换后的字符串 as begin
declare @SqlStr nvarchar(4000卡塔尔国 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

  • ‘]=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卡塔尔(قطر‎卡塔尔(قطر‎,’a’,”卡塔尔国假若text/ntext超越8000/4000,看如下例子未有试过 declare @pos int declare
    @len int declare @str nvarchar(4000卡塔尔 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 = TEXTPTXC90(字段名卡塔尔国from 表名 where 条件 UPDATETEXT 表名.字段名 @ptrval @pos @len @str set
    @count=@count+1 end ELSE break; END select @count

SQL Server数据类型:

复制代码 代码如下:declare @t
varchar(255卡塔尔国,@c varchar(255State of Qatar 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(ntextState of Qatar类型没有超过8000(4000卡塔尔长度,才得以应用*/)
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系统的经过中,大家会日常碰到数据内容的轮换操作

declare @ptr varbinary(16State of Qatar declare @artId int declare @Position int,@len
int set @len = datalength(老字符串卡塔尔 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小说数据表中的标题字段的一部分剧情,大家理应如此写

发表评论

电子邮件地址不会被公开。 必填项已用*标注