实现在ddl触发器中再创建一个dml触发器;
通过类似方法可以扩散到储存过程中创建触发器(还未验证)
create table audit(table_name sysname, data xml) go CREATE trigger on_create_table on DATABASE for CREATE_TABLE as begin declare @x xml = EVENTDATA(); declare @tableName sysname, @schemaName sysname, @triggerName sysname; set @schemaName = @x.value(N'(/EVENT_INSTANCE/SchemaName)[1]', N'sysname'); set @tableName = @x.value(N'(/EVENT_INSTANCE/ObjectName)[1]', N'sysname'); set @triggerName = @tableName + N'_trg'; declare @sql nvarchar(max); set @sql = N'create trigger ' +quotename(@schemaName) + N'.' + quotename(@triggerName) + N' on ' + quotename(@tableName) + N' after INSERT as set nocount on; declare @event xml; set @event = (select * from INSERTED for xml auto, type); insert into audit (table_name, data) values (' + quotename(@tableName, '''') +N', @event);'; -- print @sql; execute sp_executesql @sql; end go create table t (a int); go insert into t (a) values (1) go select * from audit; go