我按照Azure门户中使用ONLINE=ON
标志的查询洞察刀片的建议,向SQL Azure数据库添加新索引。 SQL看起来像这样:
CREATE NONCLUSTERED INDEX [IX_MyIndex] ON
[Customers].[Activities] ([CustomerId])
INCLUDE ([AccessBitmask], [ActivityCode], [DetailsJson],
[OrderId], [OperationGuid], [PropertiesJson], [TimeStamp])
WITH (ONLINE = ON)"
但是,我们还需要将相同的索引添加到我们的本地开发数据库中,这些数据库只是localdb
不支持ONLINE=ON
选项的实例,从而导致以下错误。< / p>
Online index operations can only be performed in Enterprise edition of SQL Server.
我的问题是 - 有没有办法编写这个SQL索引创建语句,如果可用的话,将使用ONLINE=ON
,但仍然可以在不支持它的数据库上成功?
答案 0 :(得分:3)
您可以使用以下内容:
DECLARE @Edition NVARCHAR(128)
SET @Edition = SELECT SERVERPROPERTY ('Edition')
IF @Edition LIKE 'Enterprise Edition%' OR @Edition LIKE 'SQL Azure%' BEGIN
CREATE NONCLUSTERED INDEX [IX_MyIndex] ON
[Customers].[Activities] ([CustomerId])
INCLUDE ([AccessBitmask], [ActivityCode], [DetailsJson],
[OrderId], [OperationGuid], [PropertiesJson], [TimeStamp])
WITH (ONLINE = ON)
END ELSE BEGIN
CREATE NONCLUSTERED INDEX [IX_MyIndex] ON
[Customers].[Activities] ([CustomerId])
INCLUDE ([AccessBitmask], [ActivityCode], [DetailsJson],
[OrderId], [OperationGuid], [PropertiesJson], [TimeStamp])
END