Home > Database > Mysql Tutorial > body text

数据库全文搜索批量替换字符串

WBOY
Release: 2016-06-07 15:36:18
Original
1602 people have browsed it

--如果是第一次执行则先创建存储过程 --如果不是第一次执行则直接修改 就可以。或者直接调用 create proc Full_Search(@string nvarchar(500),@string2 nvarchar(500)) --alter proc dbo.Full_Search(@string nvarchar(500),@string2 nvarchar(500)) as begi

--如果是第一次执行则先创建存储过程
--如果不是第一次执行则直接修改 就可以。或者直接调用

create proc Full_Search(@string nvarchar(500),@string2 nvarchar(500))
--alter proc dbo.Full_Search(@string nvarchar(500),@string2 nvarchar(500))
as
 begin

 declare @tbname varchar(500)
 declare tbroy cursor for select name from sysobjects
 where xtype= 'u ' --第一个游标遍历所有的表
 
 open tbroy
 fetch next from tbroy into @tbname
 while @@fetch_status=0
  begin
  
  declare @colname varchar(50)
  declare colroy cursor for select name from syscolumns
  where id=object_id(@tbname) and xtype in (
  select xtype from systypes
  where name in ( 'varchar ', 'nvarchar ', 'char ', 'nchar ') --数据类型为字符型的字段
                --where name in ('nvarchar ' ) --数据类型为字符型的字段
  ) --第二个游标是第一个游标的嵌套游标,遍历某个表的所有字段
  
  open colroy
  fetch next from colroy into @colname
  while @@fetch_status=0
   begin

   declare @sql nvarchar(1000),@j int
                      

   exec( 'UPDATE ' +@tbname + ' SET '+@colname+' = replace('+@colname+','+ ''''+@string+ ''', '+ ''''+@string2+ ''');')
                 
                        --print( 'UPDATE ' +@tbname + ' SET '+@colname+' = replace('+@colname+','+ ''''+@string+ ''', '+ ''''+@string2+ ''');')
                       -- exec( 'select distinct '+@colname+' from ' +@tbname + ' where '+ @colname+ ' like '+ '''%'+@string+ '%''')
   fetch next from colroy into @colname
   end

  close colroy
  deallocate colroy
  
  fetch next from tbroy into @tbname
  end
 close tbroy
 deallocate tbroy

 end
go
--exec Full_Search 'mm',''  整库替换函数  第一个参数是 被替换的数据 ,第二个参数是替换后的数据
go

 

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template