我有一个相当基本的 SQL 脚本来重建数据库中各种模式下的所有表索引。该脚本似乎适用于我拥有的 183 个索引,但返回错误消息
(183 row(s) affected)
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'Group'
谁能解释原因并提供解决方案?
USE RedGateMonitor;
GO
declare @db varchar(150)
declare @tmp TABLE(recnum int IDENTITY (1,1), tableschema varchar(150), tablename varchar(150))
insert @tmp (tableschema, tablename)
SELECT TABLE_SCHEMA, TABLE_NAME FROM information_schema.tables where TABLE_TYPE = 'BASE TABLE'
ORDER By TABLE_SCHEMA
declare @X int, @table varchar(150), @cmd varchar(500), @schema varchar(150)
set @X = 1
While @X <= (select count(*) from @tmp) BEGIN
set @db = 'RedGateMonitor'
set @table = (select tablename from @tmp where recnum = @X)
set @schema = (select tableschema from @tmp where recnum = @X)
set @cmd = 'ALTER INDEX ALL ON ' + @db + '.' + @schema + '.' + @table + ' REBUILD'
EXECUTE(@cmd)
set @X = @X + 1
END