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