当前位置: 代码迷 >> Sql Server >> 求大神急怎么用sql循环查找这些数据库的所有表,里面的所有字段包含的值
  详细解决方案

求大神急怎么用sql循环查找这些数据库的所有表,里面的所有字段包含的值

热度:71   发布时间:2016-04-24 18:55:42.0
求大神急!!!!如何用sql循环查找这些数据库的所有表,里面的所有字段包含的值
我想在数据库查找一个值  但是不知道这个值在哪张表里面,也不知道在哪个字段里面
想用sql写个 查找 输入值  就能知道在哪个表的哪个字段 包含了这个值

求大神 怎么写这个sql
------解决方案--------------------
用游标遍历syscolumns表然后组织语句应该可以实现,不过会给数据库造成很大压力吧
------解决方案--------------------
好高深的问题,自己不懂,看观看,留个脚印!!!
------解决方案--------------------
2008的话用这个:先创建存储过程,然后最后一句是执行的例子:
--2008查找某数据库中的列是否存在某个值
create proc spFind_Column_In_DB
(
@type int,--类型:1为文字类型、2为数值类型
@str nvarchar(100)--需要搜索的名字
)
as
--创建临时表存放结果
create table #tbl(PK int identity primary key ,tbl sysname,col sysname)
declare @tbl nvarchar(300),@col sysname,@sql nvarchar(1000)
if @type=1 
begin
declare curTable cursor fast_forward
for 
select '['+SCHEMA_NAME(SCHEMA_ID)+'].['+o.name+']' tableName,'['+c.name+']' columnName from sys.columns c inner join sys.objects o on c.object_id=o.object_id
where o.type_desc='user_table' and user_type_id in (167,175,231,239,35,99)
  end
else
begin 
declare curTable cursor fast_forward
for 
select '['+SCHEMA_NAME(SCHEMA_ID)+'].['+o.name+']' tableName,'['+c.name+']' columnName from sys.columns c inner join sys.objects o on c.object_id=o.object_id
where o.type_desc='user_table' and user_type_id in (56,48,52,59,60,62,106,108,122)
end
open curtable
fetch next from curtable into @tbl,@col
while @@FETCH_STATUS=0
begin
set @sql='if exists (select * from '+@tbl+' where '
if @type=1
begin
set @sql += @col + ' like ''%'+@str +'%'')'
end
else 
begin
set @sql +=@col + ' in ('+@str+'))'
end

set @sql += ' INSERT #TBL(tbl,col) VALUES('''+@tbl+''','''+@col+''')'
--print @sql
exec (@sql)
fetch next from curtable into @tbl,@col
end
close curtable 
deallocate curtable
select * from #tbl

--使用例子,查询库中存在aaa这个值的列:
exec  spFind_Column_In_DB  1,'aaa'

------解决方案--------------------
试试这个:

if OBJECT_ID('temp_search_table') is not null
   drop table temp_search_table
go

--存放找到的表、列、值
create table temp_search_table 
(
table_name nvarchar(100),
column_name nvarchar(100),
column_search_value nvarchar(max)
)
go


declare @sql nvarchar(max);
declare @search_str nvarchar(100);

set @sql = ''
set @search_str = '%163.com%';   --你要找的值

select @sql = @sql + 'insert into temp_search_table '+
                     'select '''+t.name +''' as table_name,''' + 
                     c.name+ ''' as column_name, ['+
                     c.name + '] from ['+t.name + 
                     '] where ['+c.name +'] like '''+@search_str+''';'
       
from sys.tables t
inner join sys.columns c
        on t.object_id = c.object_id 
inner join sys.types tp
        on c.system_type_id = tp.system_type_id
           and c.user_type_id = tp.user_type_id
           and tp.name in ('char','varchar','nchar','nvarchar')
where t.name <> 'temp_search_table'


--select @sql

exec(@sql)


--最后查找,你找到的内容
select *
from temp_search_table

------解决方案--------------------
现在创建一个表,插入值:
--drop table xxx

create table xxx(id int,abc varchar(100),xxx varchar(100))

insert into xxx
select 1,'163.com','.com' union all
select 2,'xxx','163.com'


运行上面的查找代码,会返回这样的查询结果:
/*table_name	column_name	column_search_value
xxx abc 163.com
xxx xxx 163.com*/

------解决方案--------------------
引用:
试试这个:

if OBJECT_ID('temp_search_table') is not null
   drop table temp_search_table
go
  相关解决方案