查找视图中使用的别名的真实列名?

时间:2012-04-06 19:07:03

标签: sql sql-server tsql

假设我有一个视图,其中一些列名是别名,例如本例中的“surName”:

CREATE VIEW myView AS
    SELECT  
            firstName,
            middleName,
            you.lastName surName
    FROM 
            myTable me
            LEFT OUTER JOIN yourTable you
            ON me.code = you.code
GO

我可以使用INFORMATION_SCHEMA视图检索有关视图的一些信息 例如,查询

SELECT column_name AS ALIAS, data_type AS TYPE
FROM information_schema.columns 
WHERE table_name = 'myView'

的产率:

 ----------------
|ALIAS     |TYPE |
 ----------------
|firstName |nchar|
|middleName|nchar|
|surName   |nchar|
 ----------------

但是,我想知道实际的列名。理想的情况是:

 ---------------------------
|ALIAS     |TYPE |REALNAME  |
 ---------------------------
|firstName |nchar|firstName |
|middleName|nchar|middleName|
|surName   |nchar|lastName  |
 ---------------------------

如何根据别名确定实际列名称是什么?必须有某种方法可以使用sys表和/或INFORMATION_SCHEMA视图来检索此信息。


修改 我可以接受这种令人厌恶的憎恶,这与Arion的答案类似:

SELECT
    c.name AS ALIAS,
    ISNULL(type_name(c.system_type_id), t.name) AS DATA_TYPE,
    tablecols.name AS REALNAME
FROM 
    sys.views v
    JOIN sys.columns c ON c.object_id = v.object_id
    LEFT JOIN sys.types t ON c.user_type_id = t.user_type_id
    JOIN sys.sql_dependencies d ON d.object_id = v.object_id 
        AND c.column_id = d.referenced_minor_id
    JOIN sys.columns tablecols ON d.referenced_major_id = tablecols.object_id 
        AND tablecols.column_id = d.referenced_minor_id 
        AND tablecols.column_id = c.column_id
WHERE v.name ='myView'

这会产生:

 ---------------------------
|ALIAS     |TYPE |REALNAME  |
 ---------------------------
|firstName |nchar|firstName |
|middleName|nchar|middleName|
|surName   |nchar|code      |
|surName   |nchar|lastName  |
 ---------------------------

但第三条记录是错误的 - 使用“JOIN”子句创建的任何视图都会发生这种情况,因为有两列具有相同的“column_id”,但在不同的表中。

4 个答案:

答案 0 :(得分:12)

鉴于这种观点:

CREATE VIEW viewTest
AS
SELECT
    books.id,
    books.author,
    Books.title AS Name
FROM
    Books

我可以看到你可以获得使用的列和执行此操作所使用的表:

SELECT * 
FROM INFORMATION_SCHEMA.VIEW_COLUMN_USAGE AS UsedColumns 
WHERE UsedColumns.VIEW_NAME='viewTest'

SELECT * 
FROM INFORMATION_SCHEMA.VIEW_TABLE_USAGE AS UsedTables 
WHERE UsedTables.VIEW_NAME='viewTest'

这适用于sql server 2005+。请参阅参考here

修改

给出相同的观点。试试这个问题:

SELECT
    c.name AS columnName,
    columnTypes.name as dataType,
    aliases.name as alias
FROM 
sys.views v 
JOIN sys.sql_dependencies d 
    ON d.object_id = v.object_id
JOIN .sys.objects t 
    ON t.object_id = d.referenced_major_id
JOIN sys.columns c 
    ON c.object_id = d.referenced_major_id 
JOIN sys.types AS columnTypes 
    ON c.user_type_id=columnTypes.user_type_id
    AND c.column_id = d.referenced_minor_id
JOIN sys.columns AS aliases
    on c.column_id=aliases.column_id
    AND aliases.object_id = object_id('viewTest')
WHERE
    v.name = 'viewTest';

它为我返回:

columnName  dataType  alias

id          int       id
author      varchar   author
title       varchar   Name

这也在sql 2005 +

中测试过

答案 1 :(得分:5)

我认为你不能。

选择查询会隐藏对其执行的实际数据源。因为您可以查询任何内容,即查看,表格,甚至是链接的远程服务器。

答案 2 :(得分:2)

不是一个完美的解决方案;但是,可以高度准确地解析view_definition,特别是如果代码组织良好,并且“as”具有一致的别名。另外,可以在别名后解析逗号','。

值得注意的是:select子句中的final字段没有逗号,我无法排除用作注释的项目(例如在视图文本中用 - 隔行扫描)

我在下面写了一个名为'My_Table'的表,并查看相应的'vMy_Table'

select alias, t.COLUMN_name
from 
(
select VC.COLUMN_NAME, 



case when
ROW_NUMBER () OVER (
partition by C.COLUMN_NAME order by 
CHARINDEX(',',VIEW_DEFINITION,CHARINDEX(C.COLUMN_NAME,VIEW_DEFINITION))- 
CHARINDEX(VC.COLUMN_NAME,VIEW_DEFINITION)

) = 1

then 1
else 0 end
as lenDiff



,C.COLUMN_NAME as alias

 ,CHARINDEX(',',VIEW_DEFINITION,CHARINDEX(C.COLUMN_NAME,VIEW_DEFINITION)) diff1
 , CHARINDEX(VC.COLUMN_NAME,VIEW_DEFINITION) diff2

 from INFORMATION_SCHEMA.VIEW_COLUMN_USAGE VC
inner join INFORMATION_SCHEMA.VIEWS V on V.TABLE_NAME = 'v'+VC.TABLE_Name
inner join information_schema.COLUMNS C on C.TABLE_NAME = 'v'+VC.TABLE_Name
where VC.TABLE_NAME = 'My_Table'
and CHARINDEX(',',VIEW_DEFINITION,CHARINDEX(C.COLUMN_NAME,VIEW_DEFINITION))- 
CHARINDEX(VC.COLUMN_NAME,VIEW_DEFINITION) >0
)
t

where lenDiff = 1 

希望这会有所帮助,我期待您的反馈

答案 3 :(得分:2)

花了几个小时试图找到答案,并反复遇到似乎没有工作的解决方案和最终放弃的海报,我最终偶然发现了一个似乎有用的答案:

https://social.msdn.microsoft.com/Forums/windowsserver/en-US/afa2ed2b-62de-4a5e-ae70-942e75f887a1/find-out-original-columns-name-when-used-in-a-view-with-alias?forum=transactsql

我认为,以下SQL返回的正是您正在寻找的内容,它肯定能够满足我的需求,并且看起来效果也不错。

SELECT  name
    , source_database
    , source_schema
    , source_table
    , source_column
    , system_type_name
    , is_identity_column
FROM    sys.dm_exec_describe_first_result_set (N'SELECT * from ViewName', null, 1) 

sys.dm_exec_describe_first_result_set函数的文档可以在这里找到,它可以在SQL Server 2012及更高版本中使用:

https://docs.microsoft.com/en-us/sql/relational-databases/system-dynamic-management-views/sys-dm-exec-describe-first-result-set-transact-sql

链接上的海报完全归功于我自己并没有解决这个问题,但我想在此发布此内容,以防其他人搜索此信息对我发现此主题非常有用比我联系到的那个更容易。