如何在Sys.Columns中使用列名称获取父表的名称

时间:2012-09-24 12:22:45

标签: sql-server

我想获得表名。我有列名,当我尝试查看Sys.Columns表时,我得到列的匹配名称。如何获取与所需列关联的表名

6 个答案:

答案 0 :(得分:3)

SELECT OBJECT_SCHEMA_NAME(object_id) AS TableSchemaName,
       OBJECT_NAME(object_id) AS TableName
FROM sys.columns
WHERE name = 'YourColumnName'

答案 1 :(得分:1)

我希望这会有所帮助:

select t.name from sys.columns c
inner join sys.tables t
on c.object_id = t.object_id
where c.name = 'insert column name here'

答案 2 :(得分:1)

试试这个

declare @columnName As varchar(50) = 'ParentColumnName'

select t.name from sys.tables t 
join sys.columns c 
   on c.object_id = t.object_id
and c.name = @columnName

答案 3 :(得分:1)

select OBJECT_NAME(object_id) as TableName from sys.Columns where name='columnNamehere'

答案 4 :(得分:0)

select name as 'TableName' from sys.tables where object_id=
(select object_id from sys.columns where name='UserName')

答案 5 :(得分:0)

我知道这是一个老问题,但到目前为止列出的答案并没有得到视图列的父表名称,也没有列名称为列名称的新名称在父表中。

不幸的是,(至少在2008R2中)似乎即使将您的视图注册到架构,referencing_minor_id sys.dm_sql_referenced_entities(或来自sys.SQL_Modules的等效列)始终设置为sys.dm_sql_referenced_entities为零。但是,您可以检索所有引用的表(和父视图),以及使用sys.SQL_Modules(或DECLARE @obj_name nvarchar(50) = 'Table_or_View_name_here'; with obj_id as ( select object_id, name, OBJECT_SCHEMA_NAME(object_id)+'.'+name as qualified_name from sys.all_objects as o where o.name = @obj_name ), tv_columns as ( -- table or view select o.name as Obj_Name, c.* from sys.columns as c join obj_id as o on c.object_id=o.object_id ), sql_referenced_entities as ( SELECT o.name as referencing_name, o.object_id, COALESCE(NULLIF(referencing_minor_id,0),ROW_NUMBER() OVER (ORDER BY (select 'NO_SORT'))) as referencing_minor_id, referenced_server_name, referenced_database_name, referenced_schema_name, referenced_entity_name, referenced_minor_name, referenced_id, referenced_minor_id, referenced_class, referenced_class_desc, is_caller_dependent, is_ambiguous FROM obj_id as o, sys.dm_sql_referenced_entities((select qualified_name from obj_id), 'OBJECT') where referenced_minor_id<>0 ) select c.object_id as object_id, o.name as object_name, c.column_id, c.name as column_name, c2.object_id as parent_table_object_id, o2.name as parent_table_name, c2.column_id as parent_column_id, c2.name as parent_column_name -- ,c.*, -- ,c2.* from sys.columns as c join obj_id as o on c.object_id=o.object_id left outer join (sql_referenced_entities as s join sys.all_objects as o2 on s.referenced_id=o2.object_id and s.referenced_class=1 join sys.columns as c2 on s.referenced_id=c2.object_id and s.referenced_minor_id=c2.column_id ) on c.object_id=s.object_id and c.column_id=s.referencing_minor_id )查询这些表的哪些字段。但是,它不会捕获这些绑定的顺序,因此以下内容不能完全将视图列链接到表列,但它将提供近似值:

OBJECT_DEFINITION(OBJECT_ID('schema.view'))

要获得使用的真正别名,以及涉及多个字段组合的任何计算,您必须解析exec sp_helptext 'schema.view'(或可能OBJECT_DEFINITION(OBJECT_ID('schema.view')))的输出,如下所示

/* */

开始
  1. 将单引号中的内容标记为取代删除和其他遵循的规则
  2. 删除--条评论
  3. 之间的区块
  4. 删除EXEC SP_HELPTEXT 'sp_helptext'之后的所有文字,直到下一个换行符序列(请参阅FROM了解其行尾代码)
  5. [子句
  6. 中查找表/子选择别名
  7. 解析SELECT子句并使用逗号分隔,而不是行尾。
  8. 将任何连续的空格减少为单个空格字符。强制在]之前和usp_helptext_for_view之后的空格,当一个点/句点(或空白)尚未出现时。
  9. 我们将上述内容放入我们称之为table_alias.field_name
  10. 的存储过程中
  11. 查看哪些field_name有别名或只是显示为 drop table #s create table #s (id bigint identity(1,1) primary key, text nvarchar(max)) insert into #s (text) exec usp_helptext_for_view @qualified_viewname with s as (select id, text, az=(select MIN(x*(case x when 0 then null else 1 end)) FROM (VALUES (charindex(@viewfieldname,text COLLATE Latin1_General_CI_AS)), (charindex('['+@viewfieldname+']',text COLLATE Latin1_General_CI_AS)), (charindex('AS ['+@viewfieldname+']',text COLLATE Latin1_General_CI_AS)), (charindex('as '+@viewfieldname,text COLLATE Latin1_General_CI_AS)) ) AS value(x) ), NULLIF(charindex('=',text),0)) as eq --oh, the irony of how the two different styles are applied FROM #s ) SELECT @viewfieldname as ViewField, CASE eq WHEN NULL THEN IIF(az IS NULL, NULL, LEFT(text, az-2)) ELSE RIGHT(text,LENGTH(text)-eq) -- alternately ELSE CASE az WHEN NULL THEN NULL WHEN &lt;eq THEN RIGHT(text,LENGTH(text)-eq) ELSE NULL END END as ViewFieldDefinition, id as sortPosition FROM s WHERE text like '%'+@viewfieldname+'%' -- you should be able to eliminate this clause without affecting the results. ORDER BY id, @viewfieldname 。请参阅以下代码段,了解如何从定义中分隔字段别名。
  12. 视情况将视图链接到表格。
  13. appointmentInformation = appointment.objects.get(booking_id=str(request.args.get('booking_id'))) appointmentInformation.delete()