2

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

4

6 回答 6

3
SELECT OBJECT_SCHEMA_NAME(object_id) AS TableSchemaName,
       OBJECT_NAME(object_id) AS TableName
FROM sys.columns
WHERE name = 'YourColumnName'
于 2012-09-24T12:25:16.347 回答
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'
于 2012-09-24T12:25:25.157 回答
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
于 2012-09-24T12:26:39.243 回答
1
select OBJECT_NAME(object_id) as TableName from sys.Columns where name='columnNamehere'
于 2012-09-24T12:53:25.103 回答
0
select name as 'TableName' from sys.tables where object_id=
(select object_id from sys.columns where name='UserName')
于 2012-09-24T12:28:01.893 回答
0

我知道这是一个老问题,但迄今为止列出的答案并没有得到视图列的父表名称是什么,或者列是否别名为相对于父列中的列名具有新名称桌子。

不幸的是,(至少在 2008R2 中)似乎即使将您的视图注册到 Schema,referencing_minor_idof 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. 删除 之后的任何文本--,直到下一个换行序列(请参阅EXEC SP_HELPTEXT 'sp_helptext'它们的行尾代码)
  4. FROM从子句中查找表/子选择别名
  5. 解析 SELECT 子句并以逗号分隔,而不是行尾。
  6. 将任何连续的空格减少为单个空格字符。当点/句点(或空格)尚未出现时[,在前后强制使用空格。]
  7. 我们将把上面的内容放入一个我们将调用的存储过程中usp_helptext_for_view
  8. 查看哪些table_alias.field_name是别名或仅显示为field_name. 请参阅下面的代码片段以了解如何将字段别名与定义分开。
  9. 根据需要将视图链接到表。

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

于 2017-11-08T20:05:49.027 回答