检查SQL Server 2005中的编译视图

时间:2018-03-29 13:31:14

标签: sql-server sql-server-2005

我的SQL Server数据库中有超过1000个视图,但有些视图有错误,因为视图中使用的表已被重命名,因此我收到错误,这很好。

我的问题是:我需要检查哪些视图有效且可以成功使用。

1 个答案:

答案 0 :(得分:0)

如果您知道哪些表已重命名且只有少数几个,您可以搜索使用旧表名的每个对象:

SELECT DISTINCT
       o.name AS Object_Name,
       o.type_desc,
       m.*
FROM   sys.sql_modules m
  INNER JOIN sys.objects o ON m.object_id = o.object_id
WHERE m.definition Like '%OLD_TABLE_NAME_HERE%';

如果您不知道哪些表已被重命名,您可以尝试使用此脚本查找无效对象,但请注意它可以返回误报

select QuoteName(OBJECT_SCHEMA_NAME(referencing_id)) + '.' + QuoteName(OBJECT_NAME(referencing_id)) AS ProblemObject,
       o.type_desc,
       ISNULL(QuoteName(referenced_server_name) + '.', '')
         + ISNULL(QuoteName(referenced_database_name) + '.', '')
         + ISNULL(QuoteName(referenced_schema_name) + '.', '')
         + QuoteName(referenced_entity_name) AS MissingReferencedObject
from   sys.sql_expression_dependencies sed
  left join sys.objects o on sed.referencing_id=o.object_id
where  (is_ambiguous = 0)
and  (OBJECT_ID(ISNULL(QuoteName(referenced_server_name) + '.', '')
       + ISNULL(QuoteName(referenced_database_name) + '.', '')
       + ISNULL(QuoteName(referenced_schema_name) + '.', '')
       + QuoteName(referenced_entity_name)
     ) IS NULL)
order by ProblemObject, MissingReferencedObject

EDIT
要获取所有有效对象的列表(有效意义不引用任何非现有对象),您需要做的就是将最后一个IS NULL转换为IS NOT NULL,如下所示

select QuoteName(OBJECT_SCHEMA_NAME(referencing_id)) + '.' + QuoteName(OBJECT_NAME(referencing_id)) AS ProblemObject,
       o.type_desc,
       ISNULL(QuoteName(referenced_server_name) + '.', '')
         + ISNULL(QuoteName(referenced_database_name) + '.', '')
         + ISNULL(QuoteName(referenced_schema_name) + '.', '')
         + QuoteName(referenced_entity_name) AS MissingReferencedObject
from   sys.sql_expression_dependencies sed
  left join sys.objects o on sed.referencing_id=o.object_id
where  (is_ambiguous = 0)
and  (OBJECT_ID(ISNULL(QuoteName(referenced_server_name) + '.', '')
       + ISNULL(QuoteName(referenced_database_name) + '.', '')
       + ISNULL(QuoteName(referenced_schema_name) + '.', '')
       + QuoteName(referenced_entity_name)
     ) IS NOT NULL)
order by ProblemObject, MissingReferencedObject

请注意,这只列出了不引用非现有对象的对象,但由于其他原因,它们可能仍然无效

EDIT
OBJECT_SCHEMA_NAME在sql server 2005中不可用 您可以在查询中省略该部分它仍然可以工作唯一的问题是结果中将省略模式名称(例如dbo。)。
如果所有对象都在同一个模式中,那么当然不是问题

select QuoteName(OBJECT_NAME(referencing_id)) AS ProblemObject,
       o.type_desc,
       ISNULL(QuoteName(referenced_server_name) + '.', '')
         + ISNULL(QuoteName(referenced_database_name) + '.', '')
         + ISNULL(QuoteName(referenced_schema_name) + '.', '')
         + QuoteName(referenced_entity_name) AS MissingReferencedObject
from   sys.sql_expression_dependencies sed
  left join sys.objects o on sed.referencing_id=o.object_id
where  (is_ambiguous = 0)
and  (OBJECT_ID(ISNULL(QuoteName(referenced_server_name) + '.', '')
       + ISNULL(QuoteName(referenced_database_name) + '.', '')
       + ISNULL(QuoteName(referenced_schema_name) + '.', '')
       + QuoteName(referenced_entity_name)
     ) IS NOT NULL)
order by ProblemObject, MissingReferencedObject