Handling objects being different for different SQL Server Versions

左心房为你撑大大i 提交于 2019-12-08 08:37:02

问题


I have created a new database project in Visual Studio and have been adding my pre-existing objects to it. This is a database that we'll be deploying to multiple different servers with at least two different versions of sql server.

I have a view that looks at availability groups and pulls the Is_Distributed column if the server is 2016. I need a way in this project to essentially save both versions, the 2014 and 2016, and be able to potentially deploy the correct version depending on what server i'm deploying to.

Is this possible to do in a Database Project in Visual Studios?


回答1:


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 


来源:https://stackoverflow.com/questions/55956319/handling-objects-being-different-for-different-sql-server-versions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!