1

我试图打开事务然后删除一条记录,现在我需要将删除的记录插入到事件表中。问题是我看不到结果,因为它已被删除。

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

我需要找到一种在删除后访问数据的方法,以便我可以插入到事件表中。

4

1 回答 1

0

假设您使用的是 SQL Server 2005 或更高版本,您可以使用output clause访问虚拟“已删除”表并将其内容存放在表变量中。类似于以下内容

DECLARE @TmpTable TABLE (ID INT, ......)

DELETE
FROM access_types
OUTPUT Deleted.ID,... INTO @TmpTable 

select * from @TmpTable
于 2013-04-25T15:41:37.520 回答