2016-05-26 153 views
2

我想这取决于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的条件不满足。但它仍然会产生以下错误:

Online index operations can only be performed in Enterprise edition of SQL Server.

是否有可能克服它?

+1

您确定错误信息来自您发布的代码段吗?你能检查一下SERVERPROPERTY('EngineEdition')在你的SQL Server Express实例上(它应该是4)吗? – CadentOrange

回答

4

无法在Express版中引用企业功能,它不会在语法上进行验证。您可以使用动态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