SQL帮助 - 找到以'somefieldId'为主键的表?

时间:2010-05-13 17:31:10

标签: sql sql-server search

如何在sql数据库中搜索包含字段tiEntityId的表。此字段在存储过程中引用,但我无法识别此ID是哪个表的主键?有什么建议?我目前通过使用类似这样的内容来查看存储过程定义以获取对文本的引用:

Declare @Search varchar(255)
SET @Search='[10.10.100.50]'

SELECT DISTINCT
o.name AS Object_Name,o.type_desc
FROM sys.sql_modules        m 
    INNER JOIN sys.objects  o ON m.object_id=o.object_id
WHERE m.definition Like '%'+@Search+'%'
ORDER BY 2,1

任何SQL专家知道我需要使用什么来查找包含该字段的表,最好是该字段是主键的表。

2 个答案:

答案 0 :(得分:2)

你可以这样做:

select table_name
from INFORMATION_SCHEMA.COLUMNS
where column_name = 'MyColumn'

答案 1 :(得分:1)

如果您正在寻找包含具有给定名称的列的主键(在SQL 2005+中),请转到:

select so.name as TableName,
       si.name as IndexName,
       sc.name as ColumnName
  from sys.indexes si
  join sys.index_columns sic
    on si.object_id = sic.object_id
   and si.index_id = sic.index_id
  join sys.columns sc
    on si.object_id = sc.object_id
   and sic.column_id = sc.column_id
  join sys.objects so
    on si.object_id = so.object_id
 where sc.name like '%ColumnName%'
   and si.is_primary_key = 1