以下是我动态创建Trigger
的代码的一部分。我在这里遇到的问题是当我使用Agent Job
运行此脚本时,触发器中的QUOTED_IDENTIFIER
默认设置为OFF
。但是,当我自己将其作为adhoc查询运行时,触发器以SET QUOTED_IDENTIFIER ON
选项结束。这是理想的行为。
SET @cmd3 = N'EXEC ' + @dbName + '..sp_executesql N''
CREATE TRIGGER TRG_LogAudit ON DATABASE
FOR
-- Procedures
CREATE_PROCEDURE, ALTER_PROCEDURE, DROP_PROCEDURE,
-- Tables
CREATE_TABLE, ALTER_TABLE, DROP_TABLE,
-- Views
CREATE_VIEW, ALTER_VIEW, DROP_VIEW,
-- Functions
CREATE_FUNCTION, ALTER_FUNCTION, DROP_FUNCTION,
-- Schemas
CREATE_SCHEMA, ALTER_SCHEMA, DROP_SCHEMA,
-- Triggers
CREATE_TRIGGER, ALTER_TRIGGER, DROP_TRIGGER,
-- Others
RENAME
AS
BEGIN
SET NOCOUNT ON;
DECLARE @EventData XML = EVENTDATA();
DECLARE @ip VARCHAR(32) = ( SELECT client_net_address
FROM sys.dm_exec_connections
WHERE session_id = @@SPID
);
INSERT meta.LogAudit
(EventType
,EventDDL
,EventXML
,DatabaseName
,SchemaName
,ObjectName
,HostName
,IPAddress
,ProgramName
,LoginName
)
SELECT @EventData.value(''''(/EVENT_INSTANCE/EventType)[1]'''', ''''NVARCHAR(100)'''')
,@EventData.value(''''(/EVENT_INSTANCE/TSQLCommand)[1]'''', ''''NVARCHAR(MAX)'''')
,@EventData
,DB_NAME()
,@EventData.value(''''(/EVENT_INSTANCE/SchemaName)[1]'''', ''''NVARCHAR(255)'''')
,@EventData.value(''''(/EVENT_INSTANCE/ObjectName)[1]'''', ''''NVARCHAR(255)'''')
,HOST_NAME()
,@ip
,PROGRAM_NAME()
,SUSER_SNAME();
END;
''';
....
如何修改我的脚本以确保触发器始终将QUOTED_IDENTIFIER
选项设置为ON
?我尝试了一些嵌套的EXEC
命令,但没有成功。