1

我有许多表,其中一些有共同点Asset_ID,获取具有共同点的表名列表的查询将是什么Asset_ID

4

3 回答 3

2

尝试select table_name from information_schema.columns where column_name='Asset_ID'

如果要根据列中数据的值获取表名,则无法通过简单查询获取表名,请尝试以下操作

declare @val_to_search varchar(50), @column_name varchar(50)
Select @val_to_search = 'ddh224', @column_name='Asset_ID'

declare tbl cursor for 
select table_name from information_schema.columns where column_name=@column_name
declare @tablename varchar(200),@qstr varchar(max)
declare @datapen table(table_name varchar(200))

open tbl
fetch tbl into @tablename
while @@fetch_status=0
begin
select @qstr='select top 1 '''+@tablename+''' from '+@tablename+' where '+ @column_name + ' =''' + @val_to_search + ''''
insert into @datapen
exec(@qstr)

fetch tbl into @tablename
end
close tbl
deallocate tbl
select * from @datapen pen
于 2013-07-03T07:27:12.853 回答
0

我总是用它来查找所有匹配的关键字,它会列出所有匹配的 sp、表、函数...

select distinct name 
from syscomments c 
join sysobjects o on c.id = o.id 
where TEXT like '%Asset_ID%' 
于 2013-07-03T09:17:56.900 回答
0

下面的查询可能会帮助你!

此查询将列出所有表以及列名!

SELECT * FROM INFORMATION_SCHEMA.COLUMNS where COLUMN_NAME LIKE '%columnName%'

此查询将列出所有表!

SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME LIKE '%tableName%'

于 2013-07-03T07:31:25.597 回答