动态查询触发器创建时的不同行为

时间:2017-01-30 11:15:54

标签: tsql sql-server-2016 dynamicquery

以下是我动态创建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命令,但没有成功。

0 个答案:

没有答案