MSSQL Server数据库内容替换方法

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文章数据表中的标题字段的片段内容,我们应当如此写

/*
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(1000卡塔尔国,
–目的表名 @TargetField nvarchar(1000卡塔尔国, –目的字段名 @PKFieldnvarchar(1000卡塔尔(قطر‎, –该表主键字段名 @otxt nvarchar(1000卡塔尔国,
–要求替换的字符串 @ntxt nvarchar(1000State of Qatar –替换后的字符串 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

复制代码 代码如下: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(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

UPDATE [iwms_news] SET [title] =
REPLACE([title],老字符串,新字符串State of Qatar下边包车型地铁sql语句在iwms后台的sql实践里面能够直接施行,基本上能够化解全部的替换操作,可是由于ntext数据长度的原因,这一情势对ntext类型字段无效。那大家该用怎样方法替换ntext类型字段的内容吗?方法有三种:

  • ‘]=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(4000State of Qatar declare @des nvarchar(4000卡塔尔国 declare
    @count int set @des =’requested_amount+1′–要替换来的值 set
    @len=len(@desState of Qatar 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 = TEXTPTHighlander(字段名)from 表名 where 条件 UPDATETEXT 表名.字段名 @ptrval @pos @len @str set
    @count=@count+1 end ELSE break; END select @count

二是SQL Server存储进程

上述是数据库的底子知识,是做网址的对象都应该驾驭的剧情,所以提出大家都耐性看一下。

发表评论

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

网站地图xml地图