Sql Query列出SQL Server 2005数据库中的所有视图

时间:2010-05-25 09:02:28

标签: sql sql-server metadata

我需要一个SQL查询来枚举SQL Server 2005中特定数据库的所有视图(我只需要视图名称)。提前感谢!

8 个答案:

答案 0 :(得分:87)

完成出发(已经建议):

SELECT * FROM sys.views

这为每个视图提供了额外的属性,不能从sys.objects(包含所有类型对象共有的属性)或INFORMATION_SCHEMA.VIEWS获得。虽然INFORMATION_SCHEMA方法确实提供了开箱即用的视图定义。

答案 1 :(得分:11)

SELECT SCHEMA_NAME(schema_id) AS schema_name
,name AS view_name
,OBJECTPROPERTYEX(OBJECT_ID,'IsIndexed') AS IsIndexed
,OBJECTPROPERTYEX(OBJECT_ID,'IsIndexable') AS IsIndexable
FROM sys.views

答案 2 :(得分:8)

SELECT  *
FROM    sys.objects
WHERE   type = 'V'

答案 3 :(得分:3)

在where condition。

中运行此添加DatabaseName
  SELECT TABLE_NAME, ROW_NUMBER() OVER(ORDER BY TABLE_NAME) AS 'RowNumber' 
  FROM INFORMATION_SCHEMA.VIEWS 
  WHERE TABLE_CATALOG = 'DatabaseName'

或删除添加使用条件的地方。

  use DataBaseName

  SELECT TABLE_NAME, ROW_NUMBER() OVER(ORDER BY TABLE_NAME) AS 'RowNumber' 
  FROM INFORMATION_SCHEMA.VIEWS 

答案 4 :(得分:3)

select v.name
from INFORMATION_SCHEMA.VIEWS iv
join sys.views v on v.name = iv.Table_Name
where iv.Table_Catalog = 'Your database name'

答案 5 :(得分:2)

有时您需要使用schema名称进行访问,例如您使用的是AdventureWorks数据库,您需要使用模式进行访问。

 SELECT s.name +'.'+v.name FROM sys.views v inner join sys.schemas s on s.schema_id = v.schema_id 

答案 6 :(得分:0)

不断创新。

由于您说了所有观点,从技术上讲,到目前为止所有答案都是错误的。
这是获取所有视图的方法:

SELECT 
     sch.name AS view_schema 
    ,sysv.name AS view_name 
    ,ISNULL(sysm.definition, syssm.definition) AS view_definition 
    ,create_date 
    ,modify_date 
FROM sys.all_views AS sysv 

INNER JOIN sys.schemas AS sch 
    ON sch.schema_id = sysv.schema_id 

LEFT JOIN sys.sql_modules AS sysm 
    ON sysm.object_id = sysv.object_id 

LEFT JOIN sys.system_sql_modules AS syssm 
    ON syssm.object_id = sysv.object_id 

-- INNER JOIN sys.objects AS syso ON syso.object_id = sysv.object_id 

WHERE (1=1) 
AND (sysv.type = 'V') -- seems unnecessary, but who knows
-- AND sch.name = 'INFORMATION_SCHEMA' 
/*
AND sysv.is_ms_shipped = 0  
AND NOT EXISTS 
(
    SELECT * FROM sys.extended_properties AS syscrap
    WHERE syscrap.major_id = sysv.object_id
    AND syscrap.minor_id = 0 
    AND syscrap.class = 1 
    AND syscrap.name = N'microsoft_database_tools_support' 
)  
*/

ORDER BY 
     view_schema 
    ,view_name 

答案 7 :(得分:0)

这是旧的,但是我想无论如何我都会把它解决掉,因为我找不到一个查询,该查询可以从我那里的每个视图中给我所有的SQL代码。所以这里是:

  SELECT SM.definition
  FROM sys.sql_modules SM
  INNER JOIN sys.Objects SO ON SM.Object_id = SO.Object_id
  WHERE SO.type = 'v'