更實(shí)用的清馬SQL語句
[重要通告]如您遇疑難雜癥,本站支持知識付費(fèi)業(yè)務(wù),掃右邊二維碼加博主微信,可節(jié)省您寶貴時(shí)間哦!
替換數(shù)據(jù)庫內(nèi)被掛馬的字段內(nèi)容
update 表名 set 字段名=replace(字段名,'木馬字符串','') where id=10
---------------------------
SQL Server 企業(yè)管理器
---------------------------
[Microsoft][ODBC SQL Server Driver][SQL Server]函數(shù) replace 的參數(shù) 1 的數(shù)據(jù)類型 ntext 無效。
---------------------------
確定 幫助
---------------------------
出現(xiàn)以上錯(cuò)誤的時(shí)候可以用以下語句進(jìn)行替換!
update city set city=Replace(Cast(city as varchar(8000)),'','')
要替換整個(gè)庫內(nèi)所有表所有列的馬還有更好的辦法!
方法如下:
1:sp_msforeachtable 用來loop表中的所有列
2:更新類型為ntext,text類型的列時(shí),先判斷DATALENGTH(Column)是否大于8000字節(jié),如果小于8000字節(jié)的話,我們可以使用
update Table set Column=Replace(Cast(Column as varchar(8000)),'oldkeyword','newkeyword')來更新。
源碼如下:
使用方法:在當(dāng)前數(shù)據(jù)庫使用查詢分析器建立兩個(gè)存儲過程,然后執(zhí)行下面的命令即可!
存儲過程一:UpdateTextColumn
---------------------------------------------------------------------------------------------------
Create proc [dbo].[UpdateTextColumn]
@Table varchar(100),
@Columns varchar(200),--eg:Column1,Column2,
@old varchar(100),
@new varchar(100)
as
set nocount on
declare @sql nvarchar(2000)
declare @Column varchar(50)
declare @cpos int,@npos int
set @cpos=1;
set @npos=1;
set @npos=charindex(',',@Columns,@cpos);
while(@npos>0)
begin
set @Column = substring(@Columns,@cpos,@npos-@cpos);
set @cpos = @npos+1
set @npos=charindex(',',@Columns,@cpos);
set @sql = 'update '+@Table+' set '+@Column+'=replace(cast('+@Column+' as varchar(8000)),@old,@new) where Datalength('+@Column+')<=8000';
EXECUTE sp_executesql @Sql,
N'@old varchar(100),@new varchar(100)',
@old,
@new
declare @ptr binary(16) ,@offset int,@dellen int
set @dellen = len(@old)
set @offset = 1
while @offset>=1
begin
set @offset = 0
set @sql = 'select top 1 @offset = charindex('''+@old+''' , '+@Column+'), @ptr = textptr('+@Column+') from '+@Table+' where Datalength('+@Column+')>8000 and '+@Column+' like ''%'+@old+'%''';
EXEC sp_executesql @Sql,N'@offset int OUTPUT,@ptr binary(16) OUTPUT,@old varchar(100)',
@offset OUTPUT,@ptr OUTPUT,@old;
if @offset > 0
begin
set @offset = @offset-1
set @sql='updatetext '+@Table+'.'+@Column+' @ptr @offset @dellen @new';
EXEC sp_executesql @Sql,N'@offset int ,@ptr binary(16),@dellen int,@new varchar(100)',@offset,@ptr,@dellen,@new;
end
end
end
go
---------------------------------------------------------------------------------------------------
存儲過程二:ReplaceKeyWord
---------------------------------------------------------------------------------------------------
Create proc [dbo].[ReplaceKeyWord]
@old nvarchar(100),
@new nvarchar(100)
as
declare @sql nvarchar(1000)
set @sql=N'
declare @s nvarchar(4000),@tbname sysname
select @s=N'''',@tbname=N''?''
select @s=@s+N'',''+quotename(a.name)+N''=replace(''+quotename(a.name)+N'',N'''''+@old+''''',N'''''+@new+''''')''
from syscolumns a,systypes b
where a.id=object_id(@tbname)
and a.xusertype=b.xusertype
and b.name like N''%char''
if @@rowcount>0
begin
set @s=stuff(@s,1,1,N'''')
exec(N''update ''+@tbname+'' set ''+@s)
end '
--print @sql
exec sp_msforeachtable @sql;
set @sql=N'
declare @s nvarchar(4000),@tbname sysname
select @s=N'''',@tbname=N''?''
select @s=@s+quotename(a.name)+N'',''
from syscolumns a,systypes b
where a.id=object_id(@tbname)
and a.xusertype=b.xusertype
and b.name like N''%text''
if @@rowcount>0
begin
exec UpdateTextColumn @tbname,@s,'''+@old+''','''+@new+'''
end
' ;
exec sp_msforeachtable @sql
go
---------------------------------------------------------------------------------------------------
使用方法如下:Exec ReplaceKeyWord 'www.aaa.com','www.bbb.cn'
問題未解決?付費(fèi)解決問題加Q或微信 2589053300 (即Q號又微信號)右上方掃一掃可加博主微信
所寫所說,是心之所感,思之所悟,行之所得;文當(dāng)無敷衍,落筆求簡潔。 以所舍,求所獲;有所依,方所成!