sql 查询字段内容在那个表中出现呢?

来源:百度知道 编辑:UC知道 时间:2024/06/03 12:45:22
例如:AA数据库中有ABCD4个表,我想找出a1的内容在那个表出现和那个字段出现呢?

我明白你的意思,用存储过程吧:
在当前数据库的所有表,所有字段查找字符串
/*
search a string in all databaes tables and all fields
*/

if object_id('search_db') is not null
drop proc search_db
go

create proc search_db
@table nvarchar(100),
@cond nvarchar(512)
as
declare hCForEach cursor global for
select sqlstmt =
'if exists (' + stmt + ')
print ''' + replace(stmt, '''', '''''') + ''''
from(
select stmt='select * from [' + TABLE_NAME + '] where convert(nvarchar, [' + COLUMN_NAME + ']) like ''' + @cond + ''''
from INFORMATION_SCHEMA.COLUMNS A
where (IsNull(@table, '') = '' or TABLE_NAME like @table) and DATA_TYPE <> 'image' and
(SELECT TABLE_TYPE from INFORMATION_SCHEMA.TABLES B where A.TABLE_NAME = B.TABLE_N