1.Oracle:
select table_name
from user_tab_columns
where COLUMN_NAME= 'SAL'---查询数据库内所有包含sal列的表名
2.SqlServer
select table_name
from INFORMATION_SCHEMA.COLUMNS
where COLUMN_NAME= 'ksmc'
INTERSECT
select b.name
from sysindexes a ,
sysobjects b
where a.id = b.id
and a.indid < 2
and a.rowcnt > 0
and objectproperty(b.id, 'IsMSShipped') = 0-------查询数据库内包含某个列名,并且存在数据的表名
从整个数据库内查询含有某个字符串的表名(工作量极大,慎用)
declare @cloumns varchar(40)
declare @tablename varchar(40)
declare @str varchar(40)
declare @counts int
declare @sql nvarchar(2000)
declare MyCursor Cursor For
Select a.name as Columns, b.name as TableName from syscolumns a,sysobjects b,systypes c
where a.id = b.id
and b.type = 'U'
and a.xtype=c.xtype
and c.name like '%char%'
set @str='林丽英' --此位置为查找的字符
Open MyCursor
Fetch next From MyCursor Into @cloumns,@tablename
While(@@Fetch_Status = 0)
Begin
set @sql='select @tmp_counts=count(*) from ' +@tablename+ ' where ' +@cloumns+' like ''%' +@str+ '%'''
execute sp_executesql @sql,N'@tmp_counts int out',@counts out
if @counts>0
begin
print '表名为:'+@tablename+',字段名为'+@cloumns
end
Fetch next From MyCursor Into @cloumns,@tablename
End
Close MyCursor
Deallocate MyCursor
网友评论