如果可用,如何使用在线创建非聚集索引

时间:2018-05-18 08:57:21

标签: sql sql-server azure-sql-database clustered-index

我按照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 ,但仍然可以在不支持它的数据库上成功?

1 个答案:

答案 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