查询以确定当前 SQL 服务器版本是否支持列存储索引

Query to identify if current SQL Server version supports columnstore indexes

我有一个创建一些动态 table 的存储过程。如果 SQL 服务器的主机版本支持列存储索引,那么我想创建一个列存储索引,否则回退到只创建一个普通的行存储索引。

我找到了 dm_db_persisted_sku_features table 但这只是告诉您当前正在使用哪些非标准功能,而不是支持的功能:

SELECT * FROM sys.dm_db_persisted_sku_features

如何从查询内部确定 SQL 服务器版本是否支持列存储索引?

您可以查看当前数据库的兼容级别,看是否兼容2012+的特性。

select 
  ColumnStore = case 
    when compatibility_level >= 110 
        and (serverproperty ('edition') like 'Enterprise%'
          or serverproperty ('edition') like 'Developer%')
      then 1 
    when compatibility_level >= 130 
      and serverproperty ('productlevel') != 'RTM'
      then 1 
    else 0 
    end
  from sys.databases 
  where name = db_name()

注:

SELECT * from sys.system_objects where name='column_store_dictionaries'

存在于不支持列存储索引的版本中(例如 2014 Express)