SQL查询以列出SQL Server 2005数据库中的所有视图

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

Mats asked 2020-02-22T11:45:48Z
7个解决方案
90 votes

要完成抵销(已经提出了建议):

SELECT * FROM sys.views

这将为每个视图提供额外的属性,而sys.objects(包含所有对象类型的公共属性)或INFORMATION_SCHEMA.VIEWS无法提供这些属性。 尽管INFORMATION_SCHEMA方法确实提供了现成的视图定义。

AdaTheDev answered 2020-02-22T11:46:14Z
11 votes
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
madfrag answered 2020-02-22T11:46:29Z
9 votes
SELECT  *
FROM    sys.objects
WHERE   type = 'V'
David M answered 2020-02-22T11:46:45Z
3 votes

在where条件下运行此添加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 
hgulyan answered 2020-02-22T11:47:09Z
3 votes
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'
J S answered 2020-02-22T11:47:24Z
2 votes

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

 SELECT s.name +'.'+v.name FROM sys.views v inner join sys.schemas s on s.schema_id = v.schema_id 
Elshan answered 2020-02-22T11:47:45Z
0 votes

死灵法师。

既然您说的是“所有视图”,从技术上讲,到目前为止所有答案都是错误的。
以下是获取所有视图的方法:

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 
Stefan Steiger answered 2020-02-22T11:48:14Z
translate from https://stackoverflow.com:/questions/2903262/sql-query-to-list-all-views-in-an-sql-server-2005-database