Handling objects being different for different SQL Server Versions

安稳与你 提交于 2019-12-08 08:46:26

One solution would be to create a compatibility view that works for either then reference that instead of sys.availability_groups.

In the below on 2016 the is_distributed will be pulled from the DMV but on 2014 as there is no such column in the DMV it will be pulled from OptionalColumns in the outer scope and be NULL instead.

CREATE VIEW availability_groups_compat
AS
  SELECT ca.*
  FROM   (VALUES(CAST(NULL AS BIT))) OptionalColumns(is_distributed)
         CROSS APPLY (SELECT group_id,
                             name,
                             resource_id,
                             resource_group_id,
                             failure_condition_level,
                             health_check_timeout,
                             automated_backup_preference,
                             automated_backup_preference_desc,
                             version,
                             basic_features,
                             dtc_support,
                             db_failover,
                             is_distributed,
                             cluster_type,
                             cluster_type_desc,
                             required_synchronized_secondaries_to_commit,
                             sequence_number
                      FROM   sys.availability_groups) ca 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!