从sp_msForEachTable运行时,由于QUOTED_IDENTIFIER,ALTER INDEX失败

Ian*_*oyd 10 sql-server sql-server-2008-r2 reindex sp-msforeachtable

当我尝试在表上重建索引时:

ALTER INDEX ALL ON [dbo].[Allocations] REBUILD
Run Code Online (Sandbox Code Playgroud)

工作正常.

但是当我打电话时

EXECUTE sp_msForEachTable 'ALTER INDEX ALL ON ? REBUILD'
Run Code Online (Sandbox Code Playgroud)

我到达同一张桌子,它失败了:

消息1934,级别16,状态1,行2
ALTER INDEX失败,因为以下SET选项具有不正确的设置:'QUOTED_IDENTIFIER'.验证SET选项是否正确,以便与计算列和/或筛选索引和/或查询通知和/或XML数据类型方法和/或空间索引操作的索引视图和/或索引一起使用.


并确认它是相同的表:

EXECUTE sp_msForEachTable 'print ''Rebuilding ?'';
ALTER INDEX ALL ON ? REBUILD;
PRINT ''   Done ?'''
Run Code Online (Sandbox Code Playgroud)

给出了结果:

Rebuilding [dbo].[SystemConfiguration]
   Done [dbo].[SystemConfiguration]
Rebuilding [dbo].[UserGroups]
   Done [dbo].[UserGroups]
Rebuilding [dbo].[Groups]
   Done [dbo].[Groups]
Rebuilding [dbo].[UserPermissions]
   Done [dbo].[UserPermissions]
Rebuilding [dbo].[AllocationAdmins]
   Done [dbo].[AllocationAdmins]
Rebuilding [dbo].[Allocations]
Msg 1934, Level 16, State 1, Line 2
ALTER INDEX failed because the following SET options have incorrect settings: 'QUOTED_IDENTIFIER'. Verify that SET options are correct for use with indexed views and/or indexes on computed columns and/or filtered indexes and/or query notifications and/or XML data type methods and/or spatial index operations.
Run Code Online (Sandbox Code Playgroud)

我做错了什么?


注意:

EXECUTE sp_msForEachTable 'DBCC DBREINDEX(''?'')' 
Run Code Online (Sandbox Code Playgroud)

工作正常!

Dam*_*ver 21

引用的标识符设置存储在每个存储过程中,并将sp_MSforeachtable其定义为OFF.但是,您可以解决此问题 - 通过ON在执行重新索引之前将其设置为:

create table dbo.T (
    ID int not null,
    constraint PK_T PRIMARY KEY (ID)
)
go
create view dbo.V ( ID)
with schemabinding
as
    select ID from dbo.T
go
create unique clustered index IX_V on dbo.V(ID)
go
ALTER INDEX ALL ON dbo.V REBUILD --Fine
go
exec sp_MSforeachtable  'ALTER INDEX ALL ON ? REBUILD' --Errors
go
exec sp_MSforeachtable  'SET QUOTED_IDENTIFIER ON;
ALTER INDEX ALL ON ? REBUILD' --Fine
Run Code Online (Sandbox Code Playgroud)

SET QUOTED_IDENTIFIER:

当创建一个存储过程中,SET QUOTED_IDENTIFIERSET ANSI_NULLS设置被捕获并用于该存储过程的后续调用.


当然,插入关于sp_MSforeachtable未记录的常见警告,因此您不能依赖其任何稳定的行为.


对于DBCC DBREINDEX- 所有投注都已关闭.DBCC生活在自己的小型,非常定制的代码世界中.但是,当然,它不应该依赖于未来的工作:

将在Microsoft SQL Server的未来版本中删除此功能.请勿在新的开发工作中使用此功能,并尽快修改当前使用此功能的应用程序.请ALTER INDEX改用.


Ric*_*iwi 5

您也需要SET QUOTED_IDENTIFIER ONin sp_msForEachTable,因为sp_msForEachTable没有正确的设置。

EXECUTE sp_msForEachTable 'SET QUOTED_IDENTIFIER ON; ALTER INDEX ALL ON ? REBUILD;'
Run Code Online (Sandbox Code Playgroud)

  • 如果会话默认值没有正确设置,您可能会认为直接的“ALTER INDEX”调用也会失败。 (3认同)