Quoted_Identifier SQL Server - 关闭 - 不确定如何

时间:2015-03-03 21:47:11

标签: sql-server

我们每隔几个月就会遇到一个问题,因为quoted_Identifier问题会突然导致作业或程序失败。 proc或even表中引用的标识符将从1更改为0,我们不会在该记录上看到最近修改的日期。我们不确定这是如何发生的,因为它是如此零星,我无法复制或轻松追踪它。关于为什么会发生这种情况或我能做些什么来找出的任何想法。到目前为止,我已经做了很多没有运气的研究。

由于

2 个答案:

答案 0 :(得分:0)

您可以创建由CREATE / ALTER PROCEDURE事件激活的DDL触发器。在此触发器中,您可以使用EVENTDATE函数来获取有关SQL语句执行的信息,包括ANSI_NULLS和QUOTED_IDENTIFIER设置。

例如,您可以使用Adventure Works OLTP database中的ddlDatabaseTriggerLog ddl触发器,从当前数据库中插入[dbo].[DatabaseLog]所有ddl更改。

DDL触发器:

CREATE TRIGGER [ddlDatabaseTriggerLog] ON DATABASE 
FOR DDL_DATABASE_LEVEL_EVENTS AS 
BEGIN
    SET NOCOUNT ON;
    SET ANSI_WARNINGS ON;

    DECLARE @data XML;
    DECLARE @schema sysname;
    DECLARE @object sysname;
    DECLARE @eventType sysname;

    SET @data = EVENTDATA();
    SET @eventType = @data.value('(/EVENT_INSTANCE/EventType)[1]', 'sysname');
    SET @schema = @data.value('(/EVENT_INSTANCE/SchemaName)[1]', 'sysname');
    SET @object = @data.value('(/EVENT_INSTANCE/ObjectName)[1]', 'sysname') 

    IF @object IS NOT NULL
        PRINT '  ' + @eventType + ' - ' + @schema + '.' + @object;
    ELSE
        PRINT '  ' + @eventType + ' - ' + @schema;

    IF @eventType IS NULL
        PRINT CONVERT(nvarchar(max), @data);

    INSERT [dbo].[DatabaseLog] 
        (
        [PostTime], 
        [DatabaseUser], 
        [Event], 
        [Schema], 
        [Object], 
        [TSQL], 
        [XmlEvent]
        ) 
    VALUES 
        (
        GETDATE(), 
        CONVERT(sysname, CURRENT_USER), 
        @eventType, 
        CONVERT(sysname, @schema), 
        CONVERT(sysname, @object), 
        @data.value('(/EVENT_INSTANCE/TSQLCommand)[1]', 'nvarchar(max)'), 
        @data
        );
END;
GO

[DBO]。[DatabaseLog]

SET ANSI_NULLS ON
GO    
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[DatabaseLog](
    [DatabaseLogID] [int] IDENTITY(1,1) NOT NULL,
    [PostTime] [datetime] NOT NULL,
    [DatabaseUser] [sysname] NOT NULL,
    [Event] [sysname] NOT NULL,
    [Schema] [sysname] NULL,
    [Object] [sysname] NULL,
    [TSQL] [nvarchar](max) NOT NULL,
    [XmlEvent] [xml] NOT NULL,
 CONSTRAINT [PK_DatabaseLog_DatabaseLogID] PRIMARY KEY NONCLUSTERED 
(
    [DatabaseLogID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

谁/何时更改dbo.uspGetEmployeeManagers程序?

SELECT  *
FROM    [dbo].[DatabaseLog] x
WHERE   x.[Event] IN ('CREATE_PROCEDURE', 'ALTER_PROCEDURE')
AND     x.[Schema] = 'dbo'
AND     x.[Object] = 'uspGetEmployeeManagers'

XmlEvent列内容:

<EVENT_INSTANCE>
  <EventType>CREATE_PROCEDURE</EventType>
...
  <TSQLCommand>
    <SetOptions ANSI_NULLS="ON" ANSI_NULL_DEFAULT="ON" ANSI_PADDING="ON" QUOTED_IDENTIFIER="ON" ENCRYPTED="FALSE" />
    <CommandText>
CREATE PROCEDURE [dbo].[uspGetEmployeeManagers]
    @BusinessEntityID [int]
AS
...

答案 1 :(得分:0)

这些脚本很有帮助,我将来会使用它们。我弄清楚问题是什么。存储过程将quoted_identifier设置为0,并且它已经存在至少一年。但是,有人在存储过程正在使用的表上创建了筛选索引。他们没有意识到过滤后的索引需要将quoted_identifier设置为1。