如何搜索某个字符串在N个表的那个字段中,并一次性修改N个表中这个搜索到的字段的所有记录

时间:2023-01-04 11:13:13
--搜索某个字符串在那个表的那个字段中

declare @str varchar(100)
set @str='White'  --要搜索的字符串

declare @s varchar(8000)
declare tb cursor local for
select s='if exists(select 1 from ['+b.name+'] where ['+a.name+'] like ''%'+@str+'%'')
 print ''所在的表及字段: ['+b.name+'].['+a.name+']'''
from syscolumns a join sysobjects b on a.id=b.id
where b.xtype='U' and a.status>=0
 and a.xusertype in(175,239,231,167)
open tb
fetch next from tb into @s
while @@fetch_status=0
begin
 exec(@s)
 fetch next from tb into @s
end
close tb
deallocate tb

/*--测试结果

所在的表及字段: [authors].[au_lname]

--*/
上面是某个高手写的代码,这段代码可以查出某个字段在这个数据库中存在那个表中的具体字段,现在我的问题是如果这个字段在N个表中,然后我想把这个字段的内容由White改成Black,要如何修改啊。请各位高手帮帮忙。

11 个解决方案

#1


一样的要求,请参考:
http://community.csdn.net/Expert/topic/4084/4084364.xml?temp=.2177698

#2


老大写的,收藏珍品:

if exists (
select * from dbo.sysobjects 
where id = object_id(N'[dbo].[p_search]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_search]
GO

/*--搜索某个字符串在那个表的那个字段中
--邹建 2004.10(引用请保留此信息)--*/

/*--调用示例
use pubs
exec p_search N'l'
--*/
create proc p_search
@str Nvarchar(1000) --要搜索的字符串
as
if @str is null return

declare @s Nvarchar(4000)
create table #t(表名 sysname,字段名 sysname)

declare tb cursor local for
select s='if exists(select 1 from ['+replace(b.name,']',']]')+'] where ['+a.name+'] like N''%'+@str+'%'')
print ''所在的表及字段: ['+b.name+'].['+a.name+']'''
from syscolumns a join sysobjects b on a.id=b.id
where b.xtype='U' and a.status>=0 and a.xtype in(175,239,99,35,231,167)
open tb
fetch next from tb into @s
while @@fetch_status=0
begin
exec(@s)
fetch next from tb into @s
end
close tb
deallocate tb
go

#3


对不起,可能我没说清楚,我要找的是字段的值而不是字段的内容。另外,找出值后要修改成另一个值,这个程序只是找出值,并没有告诉我如何把A值转换成B值

#4


这个程序只是找出对应表中的第一个值,并不是所有的值的

#5


看来你的这个问题要复杂的多哦,楼下的高手解决啊.

#6


是有点复杂,要找出这个数据库中所有的有这个要待搜索的字符串的值的表,并修改这个字符串为另一个字符串。

#7


给个例子可以会说得清楚一点
表一城市代码表结构
fcitycode fareacode
08JD001   08JD
08JD002   08JD
表二进销存表结构
fcitycode fstockdate
08JD001    '2005-01'
08JD001    '2005-02'
08JD002    '2005-01'
表三员工工资表结构
fcitycode  femployeecode
08JD001    08JD001001
08JD001    08JD001002
08JD002    08JD002001
还有N张表中都有FCITYCODE这个字段,然后这个字段中都有08JD001这个值
我现在的问题就是想找出这个数据库中那个表中有‘08JD001'这个值,
并且一次性把这个值改为‘08SD001’,相应的FEMOPLYEECODE代码中的值‘08JD001001’,‘08JD001002’也能改为‘08SD001001’,‘08SD001002’
邹建老大的程序只是搜索出这个待搜索字段‘08JD001’,并没有告诉我如何把找到的所有‘08JD001’一次性的改为我想要的‘08SD001’,相应的员工代码‘08JD001001’等也改成‘08SD001001’

想各位高手帮帮忙了.谢谢了

#8


if exists (select * from dbo.sysobjects where id = object_id('p_replace'))
drop procedure [dbo].[p_replace]
go

if exists(select * from sysobjects where id=object_id('#'))
drop table #
go

set nocount on
go

--创建存储过程
create proc p_replace
@colname Nvarchar(1000), --要搜索的字段
@rstr nvarchar(200)      --要替换的内容
as
if @colname is null return
declare @s Nvarchar(4000)
select a.name col,b.name tb into # from syscolumns a join sysobjects b on a.id=b.id and a.name like '%'+@colname+'%'
declare cur cursor for
select * from #
open cur
declare @tbname nvarchar(20)
declare @columnname nvarchar(20)
fetch next from cur into @columnname,@tbname
while @@fetch_status=0
  begin
     set @s=N'update '+@tbname+N' set '+@columnname+'='''+@rstr+''''
     --print(@s)
     exec(@s)
     fetch next from cur into @columnname,@tbname
  end
close cur
deallocate cur
go

--测试存储过程
exec p_replace N'adduser',N'hhhhhhhhhhhhh'

#9


楼上的vivianfdlpw() 的方法不行
都没有改过来。

#10


楼上的vivianfdlpw()
你能说清楚点吗?我试了还是不行啊

#11


楼上的vivianfdlpw()
你的程序我改了一点
由 begin
     set @s=N'update '+@tbname+N' set '+@columnname+'='''+@rstr+''''
     --print(@s)
     exec(@s)
     fetch next from cur into @columnname,@tbname
  end
改成
begin
     set @s=N'update '+@tbname+N' set '+@columnname+'='''+@rstr+'''
     where  '+@columnname+'='''+@colname+''''
     print(@s)
     exec(@s)
     fetch next from cur into @columnname,@tbname
  end
加了一个where条件,基本可以,但是还有点问题,就是没有@rstr值的表也给出来了,只要有a.name 他就显示,但问题是有a.name字段并不一定有@str值,如何过滤掉这些值呢

#1


一样的要求,请参考:
http://community.csdn.net/Expert/topic/4084/4084364.xml?temp=.2177698

#2


老大写的,收藏珍品:

if exists (
select * from dbo.sysobjects 
where id = object_id(N'[dbo].[p_search]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_search]
GO

/*--搜索某个字符串在那个表的那个字段中
--邹建 2004.10(引用请保留此信息)--*/

/*--调用示例
use pubs
exec p_search N'l'
--*/
create proc p_search
@str Nvarchar(1000) --要搜索的字符串
as
if @str is null return

declare @s Nvarchar(4000)
create table #t(表名 sysname,字段名 sysname)

declare tb cursor local for
select s='if exists(select 1 from ['+replace(b.name,']',']]')+'] where ['+a.name+'] like N''%'+@str+'%'')
print ''所在的表及字段: ['+b.name+'].['+a.name+']'''
from syscolumns a join sysobjects b on a.id=b.id
where b.xtype='U' and a.status>=0 and a.xtype in(175,239,99,35,231,167)
open tb
fetch next from tb into @s
while @@fetch_status=0
begin
exec(@s)
fetch next from tb into @s
end
close tb
deallocate tb
go

#3


对不起,可能我没说清楚,我要找的是字段的值而不是字段的内容。另外,找出值后要修改成另一个值,这个程序只是找出值,并没有告诉我如何把A值转换成B值

#4


这个程序只是找出对应表中的第一个值,并不是所有的值的

#5


看来你的这个问题要复杂的多哦,楼下的高手解决啊.

#6


是有点复杂,要找出这个数据库中所有的有这个要待搜索的字符串的值的表,并修改这个字符串为另一个字符串。

#7


给个例子可以会说得清楚一点
表一城市代码表结构
fcitycode fareacode
08JD001   08JD
08JD002   08JD
表二进销存表结构
fcitycode fstockdate
08JD001    '2005-01'
08JD001    '2005-02'
08JD002    '2005-01'
表三员工工资表结构
fcitycode  femployeecode
08JD001    08JD001001
08JD001    08JD001002
08JD002    08JD002001
还有N张表中都有FCITYCODE这个字段,然后这个字段中都有08JD001这个值
我现在的问题就是想找出这个数据库中那个表中有‘08JD001'这个值,
并且一次性把这个值改为‘08SD001’,相应的FEMOPLYEECODE代码中的值‘08JD001001’,‘08JD001002’也能改为‘08SD001001’,‘08SD001002’
邹建老大的程序只是搜索出这个待搜索字段‘08JD001’,并没有告诉我如何把找到的所有‘08JD001’一次性的改为我想要的‘08SD001’,相应的员工代码‘08JD001001’等也改成‘08SD001001’

想各位高手帮帮忙了.谢谢了

#8


if exists (select * from dbo.sysobjects where id = object_id('p_replace'))
drop procedure [dbo].[p_replace]
go

if exists(select * from sysobjects where id=object_id('#'))
drop table #
go

set nocount on
go

--创建存储过程
create proc p_replace
@colname Nvarchar(1000), --要搜索的字段
@rstr nvarchar(200)      --要替换的内容
as
if @colname is null return
declare @s Nvarchar(4000)
select a.name col,b.name tb into # from syscolumns a join sysobjects b on a.id=b.id and a.name like '%'+@colname+'%'
declare cur cursor for
select * from #
open cur
declare @tbname nvarchar(20)
declare @columnname nvarchar(20)
fetch next from cur into @columnname,@tbname
while @@fetch_status=0
  begin
     set @s=N'update '+@tbname+N' set '+@columnname+'='''+@rstr+''''
     --print(@s)
     exec(@s)
     fetch next from cur into @columnname,@tbname
  end
close cur
deallocate cur
go

--测试存储过程
exec p_replace N'adduser',N'hhhhhhhhhhhhh'

#9


楼上的vivianfdlpw() 的方法不行
都没有改过来。

#10


楼上的vivianfdlpw()
你能说清楚点吗?我试了还是不行啊

#11


楼上的vivianfdlpw()
你的程序我改了一点
由 begin
     set @s=N'update '+@tbname+N' set '+@columnname+'='''+@rstr+''''
     --print(@s)
     exec(@s)
     fetch next from cur into @columnname,@tbname
  end
改成
begin
     set @s=N'update '+@tbname+N' set '+@columnname+'='''+@rstr+'''
     where  '+@columnname+'='''+@colname+''''
     print(@s)
     exec(@s)
     fetch next from cur into @columnname,@tbname
  end
加了一个where条件,基本可以,但是还有点问题,就是没有@rstr值的表也给出来了,只要有a.name 他就显示,但问题是有a.name字段并不一定有@str值,如何过滤掉这些值呢