日期:2014-05-19  浏览次数:20808 次

怎么在整个库里查找某个字段的值或字段名?
在库mydb里有某个表mytab的一个列名叫mycol,此字段某个值为myval
1、怎么查找整个数据库列出包含有列名为mycol的所有表?
2、怎么查找整个数据库列出值为myval的所有表和列?

------解决方案--------------------
1.select sysobjects.name
from syscolumns
inner join sysobjects on syscolumns.id=sysobjects.id
where syscolumns.name= 'name '
------解决方案--------------------
--1、怎么查找整个数据库列出包含有列名为mycol的所有表?
Select A.Name From SysObjects A Inner Join SysColumns B On A.ID = B.ID And B.Name = 'mycol '
------解决方案--------------------
1. select TABLE_NAME from information_schema.columns where column_name= 'mycol '
2 .全文索引
------解决方案--------------------
2: 给定一个任意的字段的值,查询出它属于的表及哪个字段的SQL语句:

declare @str varchar(100)
set @str= 'PH ' --要搜索的字符串

declare @s varchar(8000)
declare tb cursor local for
select s= 'if exists(select 1 from [ '+b.name+ '] where [ '+a.name+ '] like ' '% '+@str+ '% ' ')
print ' '所在的表及字段: [ '+b.name+ '].[ '+a.name+ '] ' ' '
from syscolumns a join sysobjects b on a.id=b.id
where b.xtype= 'U ' and a.status> =0
and a.xusertype in(175,239,231,167)
open tb
fetch next from tb into @s
while @@fetch_status=0
begin
exec(@s)
fetch next from tb into @s
end
close tb
deallocate tb