create table
(
name nvarchar(50),
rows char(50),
reserved varchar(50),
data varchar(50),
index_size varchar(50),
unused varchar(50)
)
go
insert
select * from
declare @str varchar(100)
set @str='3.5'
declare @s varchar(8000)
declare tb cursor local for
select s='if exists(select 1 from ['+b.name+'] where ['+a.name+'] like ''%'+@str+'%'')
print ''select * from ['+b.name+'] where ['+a.name+'] = "'+@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,56,60,108,106)
and b.name in (select name from
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 top 100 * from syscolumns a
【系统字段表,当前数据库所有对象的字段名】
select top 100 * from sysobjects b
【系统对象表,当前数据库所有的对象(表,视图,过程,索引,关健字,约束等)】
select top 100 * from systypes c
【字段类型表,a.xtype=c.xusertype】
xtype='U'是用户表
a.status>=0这个条件是没有用的,MSSQL 系统字段表的status都是>=0的
1.查询数据库内含字段名为[name]的所有表名
select OBJECT_NAME(id) from syscolumns where id in(
select id from sysobjects where type='U')
and name='name'
create proc spFind_Column_In_DB
(
@type int,
@str nvarchar(100)
)
as
create table
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+''')'
exec (@sql)
fetch next from curtable into @tbl,@col
end
close curtable
deallocate curtable
select * from
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'
exec(@sql)
select *
from temp_search_table
select distinct name
from sysobjects o, syscomments s
where o.id = s.id
and o.xtype = 'P'
and text like '%is_zx=''清''%'
评论已关闭