我正在尝试根据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
Run Code Online (Sandbox Code Playgroud)
在SQL Server Express上,条件IF不满足.但它仍然会产生以下错误:
联机索引操作只能在SQL Server的企业版中执行.
有可能克服它吗?
在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
Run Code Online (Sandbox Code Playgroud)