如何通過一個值查找到值所在的SQL數據庫表

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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章