查找库中所有表所有字段中某个指定的字符


DECLARE @table nvarchar(50),@clumn nvarchar(50), @sql nvarchar(2000),@oldword nvarchar(20),@newword nvarchar(20)

DECLARE cursor_name CURSOR FOR
--查看所有表对应的所有字段,user_type_id 确定了字段的类型,这里我只取字符型字段,将查出的表放在游标中。
SELECT t.name ,c.name 
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE  c.user_type_id in (231,167,239) and t.name != 'SensitiveWord'
ORDER BY  t.name
OPEN cursor_name
FETCH NEXT FROM cursor_name INTO @table ,@clumn
WHILE @@FETCH_STATUS = 0
BEGIN 
--sensitiveword存了所有的敏感词,里面有敏感词和要显示的敏感词两列。将查出的表再放入另一个游标中。
DECLARE cursor_word CURSOR FOR SELECT newword,oldword FROM sensitiveword 
OPEN cursor_word
FETCH NEXT FROM cursor_word INTO @newword,@oldword
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sql=N' update ' + @table + N' set '+ @clumn + N'=REPLACE(' + @clumn + N','''+@oldword+N''','''+@newword+N''') where ' + @clumn + N' like ''%'+@oldword+N'%'''
PRINT @sql
EXEC sp_executesql @sql
FETCH NEXT FROM cursor_word INTO @newword,@oldword
END
CLOSE cursor_word 
DEALLOCATE cursor_word 
FETCH NEXT FROM cursor_name INTO @table,@clumn

END
CLOSE cursor_name 
DEALLOCATE cursor_name 


 

需求:需要将库中所有“aaa”,替换为“axx”  将 “bbb”替换为 “bxx”

原文地址:https://www.cnblogs.com/zhengwei-cq/p/14632663.html