sql server 如何在全庫中查找數據在哪一個表

1.查找字段在庫中哪一個表sql

若是要查找FName spa

select   a.name,b.name   from   syscolumns a   inner   join   sysobjects   b   on   a.id=b.id 
where   a.name   =   ' FName'

2.查找數據值在哪一個表中table

 若是要查找 制動器組object

 

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+' = ''' +@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

 

 

 

親測可用,可是第二項查找數據會報錯,但願哪一個大神幫忙解決報錯問題。 select

相關文章
相關標籤/搜索