我尝试打开事务,然后删除一条记录,我需要将已删除的记录插入事件表。问题是我无法看到结果,因为它已被删除。
CREATE procedure [dbo].[TestData] ( @clientid bigint ) As
Begin
print ''abc''
insert into Client_Event_Log values ( getdate(),0,@clientid,100,''B0AE3162-671C-E211-AF2A-00155D051024'',NULL)
BEGIN TRY
BEGIN TRAN
Delete from access_types -- There is only record in the table.
8559230 abc 101 0 2010-01-01 10:25:25.000
select * from access_types -- cann't see the deleted record even before the session.
DECLARE @cGTAEventLog bigint
select @cGTAEventLog=Access_Type_Id from access_types
exec TestData @cGTAEventLog -- Now i am passing 8559230 to the SP to insert into event
table but it has been delete before so can't insert NULL
Commit Tran
END TRY
BEGIN CATCH
ROLLBACK TRAN
--Error message
PRINT 'Error: ' +
CONVERT(VARCHAR,ERROR_NUMBER()) + ' - ' +
CONVERT(VARCHAR,ERROR_SEVERITY()) + ' - ' +
CONVERT(VARCHAR,ERROR_STATE()) + ' - ' +
ERROR_MESSAGE() +
' Raise Error occurred at line ' + CONVERT(VARCHAR,ERROR_LINE())
END CATCH
END
我需要找到一种在删除后访问数据的方法,这样我就可以插入到事件表中了。
答案 0 :(得分:0)
假设您使用的是SQL Server 2005或更高版本,则可以使用output clause
访问虚拟的“已删除”表并将其内容驻留在表变量中。类似于以下内容
DECLARE @TmpTable TABLE (ID INT, ......)
DELETE
FROM access_types
OUTPUT Deleted.ID,... INTO @TmpTable
select * from @TmpTable