declare @cloumns varchar(40)sql
declare @tablename varchar(40)ide
declare @str varchar(40)it
declare @counts inttable
declare @sql nvarchar(2000)class
declare MyCursor Cursor For object
Select a.name as Columns, b.name as TableName from syscolumns a,sysobjects b,systypes c select
where a.id = b.idnext
and b.type = 'U' 查詢
and a.xtype=c.xtypetab
and c.name like '%char%'
set @str='5555555'
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
查詢包含「5555555」的表