回滚事务中的任何操作也会回滚。不这样做会破坏事务的原子性。鉴于您正在审核的活动正在回滚,您很可能实际上希望无论如何都回滚审核。
尽管如此,当需要记录当前事务范围之外的操作时,还是有一些合理的情况,例如某些调试情况。有已知的变通方法,例如对用户可配置的事件类使用事件通知,然后使用以使事件通知激活过程运行并记录审计。因为探查器事件是在事务范围之外生成的,所以审计记录不会回滚。sp_trace_generateevent
:setvar dbname testdb
:on error exit
set nocount on;
use master;
if exists (
select * from sys.server_event_notifications
where name = N'audit')
begin
drop event notification audit on server;
end
go
if db_id('$(dbname)') is not null
begin
alter database [$(dbname)] set single_user with rollback immediate;
drop database [$(dbname)];
end
go
create database [$(dbname)];
go
alter authorization on database::[$(dbname)] to [sa];
go
use [$(dbname)];
go
create queue audit;
create service audit on queue audit (
[http://schemas.microsoft.com/SQL/Notifications/PostEventNotification]);
go
create table audit_table (
Time datetime not null,
TextData nvarchar(256) not null);
go
create procedure usp_audit
as
begin
declare @h uniqueidentifier, @mt sysname, @mb varbinary(max), @mx xml;
begin transaction;
receive top(1) @h = conversation_handle,
@mt = message_type_name,
@mb = message_body
from audit;
if (@mt = N'http://schemas.microsoft.com/SQL/Notifications/EventNotification')
begin
select @mx = cast(@mb as xml);
insert into audit_table (Time, TextData)
values (
@mx.value(N'(/EVENT_INSTANCE/PostTime)[1]', N'datetime'),
@mx.value(N'(/EVENT_INSTANCE/TextData)[1]', N'nvarchar(256)'));
end
else if (@mt = N'http://schemas.microsoft.com/SQL/ServiceBroker/Error'
or @mt = N'http://schemas.microsoft.com/SQL/ServiceBroker/EndDialog')
begin
end conversation @h;
end
commit
end
go
alter queue audit
with activation (
status = on,
procedure_name = usp_audit,
max_queue_readers = 1,
execute as owner);
go
create event notification audit
on server for USERCONFIGURABLE_0
to service N'audit', N'current database';
go
begin transaction;
exec sp_trace_generateevent 82, N'this was inserted from a rolled back';
rollback
go
waitfor delay '00:00:05';
select * from audit_table;
go