狂人呓语

SQL Server 从数据库中查询某个值在哪个字段的方法

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='FindStr'
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

数据太多的时候查询比较慢,直接复制,直接用,对数据库无影响。

未经允许不得转载:狂人呓语 » SQL Server 从数据库中查询某个值在哪个字段的方法

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址