非企业版中的SQL Server联机索引

时间:2016-05-26 10:23:38

标签: sql-server

我正在尝试根据SQL Server版本实现逻辑:

IF SERVERPROPERTY('EngineEdition') = 3 /* Enterprise */ OR SERVERPROPERTY('EngineEdition') = 5 /* SQL Azure */
BEGIN
  CREATE NONCLUSTERED INDEX IX_MobileDeviceId_with_include ON dbo.FineActivations (MobileDeviceId) INCLUDE (ActivationTime, FineId) WITH (ONLINE = ON);
END

在SQL Server Express上,IF中的条件不满足。但它仍然会产生以下错误:

  

联机索引操作只能在SQL Server企业版中执行。

是否有可能克服它?

1 个答案:

答案 0 :(得分:6)

在Express版本中不可能引用Enterprise功能,它不会在语法上进行验证。您可以使用动态SQL来解决此问题。

DECLARE @sqlcmd nvarchar(4000)
SET @sqlcmd = ' CREATE NONCLUSTERED INDEX IX_MobileDeviceId_with_include ON dbo.FineActivations (MobileDeviceId) INCLUDE (ActivationTime, FineId) '

if SERVERPROPERTY('EngineEdition') = 3 /* Enterprise */ OR SERVERPROPERTY('EngineEdition') = 5 /* SQL Azure */
begin 
set @sqlcmd = @sqlcmd +'
    with (online = on)'
end
else
begin
set @sqlcmd = @sqlcmd + '
    with (fillfactor = 80)'
end

if ((select indexproperty(object_id('FineActivations'),'IX_MobileDeviceId_with_include','IndexID')) is null)
BEGIN
    EXEC (@sqlcmd)
END