获取视图的列描述

时间:2010-09-22 03:43:09

标签: sql sql-server tsql

是从基表(fn_listextendedproperty)中检索视图列描述的方法吗?

以下语法仅列出视图中的列名

 SELECT *
   FROM sys.columns c
   JOIN sys.views v on c.OBJECT_ID = V.OBJECT_ID
   JOIN sys.schemas s ON s.schema_id = v.schema_id
                     and V.NAME = 'v_ProductInfo'

3 个答案:

答案 0 :(得分:4)

最后我得到了答案。

SELECT VIEW_COLUMN_NAME=c.name,VIEW_CATALOG,VIEW_SCHEMA,VIEW_NAME,TABLE_CATALOG,TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME, ep.value as 'COLUMN_DESCRIPTION'
FROM sys.columns c
INNER JOIN sys.views vw on c.OBJECT_ID = vw.OBJECT_ID
INNER JOIN sys.schemas s ON s.schema_id = vw.schema_id
LEFT JOIN INFORMATION_SCHEMA.VIEW_COLUMN_USAGE vcu on vw.name = vcu.VIEW_NAME and s.name = vcu.VIEW_SCHEMA and c.name = vcu.COLUMN_NAME
LEFT JOIN (
    SELECT distinct SCM_Name=SCM.Name,TBL_Name=TBL.name,COLName=COL.name,COL_Object_id= COL.object_id,COL_column_id=COL.column_id
    FROM
    SYS.COLUMNS COL
    INNER JOIN SYS.TABLES TBL on COL.object_id = TBL.object_id
    INNER JOIN SYS.SCHEMAS SCM ON TBL.schema_id = SCM.schema_id) tempTBL on tempTBL.TBL_Name=vcu.TABLE_NAME and tempTBL.SCM_Name=TABLE_SCHEMA and tempTBL.COLName = vcu.COLUMN_NAME
Left join sys.extended_properties ep on tempTBL.COL_Object_id = ep.major_id and tempTBL.COL_column_id = ep.minor_id
where vw.NAME = 'v_ProductInfo'

答案 1 :(得分:0)

如果视图使用来自具有相同名称的不同表的两列

,则此解决方案将失败

类似的东西:

  

选择
  t1.Id,
  t2.Id为'T2Id'   从表1为t1
  内部连接表2为t2在t1.SomeCol = t2.SomeOtherCol

答案 2 :(得分:0)

是的,您可以使用cte来检索数据。

Select * from (
    SELECT Row=ROW_NUMBER() over (PARTITION by vw.NAME,Column_id order by Column_id),IEW_COLUMN_NAME=c.name,VIEW_CATALOG,VIEW_SCHEMA,VIEW_NAME,TABLE_CATALOG,TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME, ep.value as 'COLUMN_DESCRIPTION' 
    FROM sys.columns c
    INNER JOIN sys.views vw on c.OBJECT_ID = vw.OBJECT_ID
    INNER JOIN sys.schemas s ON s.schema_id = vw.schema_id
    LEFT JOIN INFORMATION_SCHEMA.VIEW_COLUMN_USAGE vcu on vw.name = vcu.VIEW_NAME and s.name = vcu.VIEW_SCHEMA and c.name = vcu.COLUMN_NAME 
    LEFT JOIN (
        SELECT distinct SCM_Name=SCM.Name,TBL_Name=TBL.name,COLName=COL.name,COL_Object_id= COL.object_id,COL_column_id=COL.column_id
        FROM
        SYS.COLUMNS COL
        INNER JOIN SYS.TABLES TBL on COL.object_id = TBL.object_id
        INNER JOIN SYS.SCHEMAS SCM ON TBL.schema_id = SCM.schema_id ) tempTBL on tempTBL.TBL_Name=vcu.TABLE_NAME and tempTBL.SCM_Name=TABLE_SCHEMA and tempTBL.COLName = vcu.COLUMN_NAME 
    Left join sys.extended_properties ep on tempTBL.COL_Object_id = ep.major_id and tempTBL.COL_column_id = ep.minor_id
    --where vw.name = 'vw_avm_allc'
) data
Where Row=1