111,126
社区成员
发帖
与我相关
我的任务
分享--> Test Data: [T1]
if object_id('[T1]') is not null drop table [T1]
create table [T1] ([编号] int,[内容] varchar(5))
insert into [T1]
select 1,'asdf' union all
select 1,'ewrer' union all
select 2,'12123' union all
select 2,'weew'
if object_id('[T2]') is not null drop table [T2]
create table [T2] ([编号] int,[内容2] varchar(5))
insert into [T2]
select 1,'abbb' union all
select 1,'脸' union all
select 2,'人人' union all
select 2,'weew'
--Code
--创建临时表
create table #tmp(所在的表及字段 varchar(100),字段内容 text)
go
create proc proc_search
@str varchar(100)
as
begin
--declare @str varchar(100)
--set @str='aa' --要搜索的字符串
declare @s varchar(8000)
declare tb cursor local for
select s='if exists(select 1 from ['+b.name+'] where ['+a.name+'] like ''%'+@str+'%'')
insert into #tmp select ''['+b.name+'].['+a.name+']'',['+a.name+'] from ['+b.name+'] where ['+a.name+'] like ''%'+@str+'%''
'
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
select * from #tmp
end
go
--执行
exec proc_search 'a'
--Drop
drop table T1
drop table T2
drop table #tmp
drop proc proc_search
--Result
/*
所在的表及字段 字段内容
---------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[T1].[内容] asdf
[T2].[内容2] abbb
*/