批量改良数据库全体表中字段的内容,中木马后的抢救管理

我们会经常遇到数据内容的替换操作,Server数据库的数据存储类型,ntext列中的字符串(不支持中文),可以把ntext字段转换为varchar,对于字段长度大于8000的ntext字段无效,declare @str varchar(500),b.name from sysobjects a

主导提示:在运用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(1000State of Qatar, –该表主键字段名 @otxt nvarchar(1000卡塔尔国,
–要求替换的字符串 @ntxt nvarchar(1000卡塔尔(قطر‎ –替换后的字符串 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(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(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

在利用iwms系统的进度中,大家会时时遭遇数据内容的更换操作。在告知大家怎么替换数据内容前面,小编提议大家先掌握一下SQL
Server数据库的数据存款和储蓄类型:

  • ‘]=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(8000State of Qatar卡塔尔国,’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(@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(16State of Qatar SELECT @ptrval = TEXTPTENCORE(字段名)from 表名 where 条件 UPDATETEXT 表名.字段名 @ptrval @pos @len @str set
    @count=@count+1 end ELSE break; END select @count

SQL Server数据类型:

如上是数据库的功底知识,是做网站的爱侣都应有知道的原委,所以提议大家都语重心长看一下。