USE MyDBName
DECLARE @IndexList NVARCHAR(MAX)
;WITH CTE_I AS
(
SELECT N'ALL_INDEXES' AS Iname, 1 AS SortOrder
UNION ALL
SELECT '-' + I.name AS Iname, 2 AS SortOrder
FROM sys.spatialindexes AS I
)
SET @IndexList = STRING_AGG(CONVERT(NVARCHAR(MAX), I.Iname), ',') WITHIN GROUP (ORDER BY SortOrder, Iname)
FROM CTE_I AS I
USE DatabaseThatHasOlasStuff
EXECUTE dbo.IndexOptimize
@Databases = 'MyDBName',
@Indexes = @IndexList
@LogToTable = 'Y'
这是您可以利用的另一种解决方案。请原谅我,因为我是在文本编辑器中编写的(手边没有实例),但这对您来说可能有用。