SQL trigger for audit table

I am writing a trigger to audit updates and deletes in tables. I am using SQL Server 2008

My questions:

Is there a way to find out what actions are taken in a record without going through the step of selecting deleted and inserted tables?

Another question: if the record is deleted, how can I record in the audit table of the user performing the deletion. (NOTE: A user connected to the database is a common connection string with the specified user, I need a user who is registered in a web application or a Windows application).

Please, help?

+3
source share
4 answers

, , INSERTED DELETED .

, , - web/windows. , , , .

, "LastModifiedBy" , , . . , , , -, , , .

+3

, , , , . , , . , .

, , - , IsDeleted , , . , isdelted null.

. , , , . ( , dba, - ), , dba, . , . , , , . .

, , //. .

+4

roufamatic, , , INSERTED DELETED.

, , . , , , , SET CONTEXT_INFO CONTEXT_INFO(), . , SET CONTEXT_INFO, , CONTEXT_INFO(). , . , SET CONTEXT_INFO . , . , - , , USERNAME() (, , , , ).

, CONTEXT_INFO , , .

, , . - CONTEXT_INFO SET CONTEXT_INFO, , , .

+3

, , INSERTED DELETED . , - . , -

+1

Source: https://habr.com/ru/post/1749067/


All Articles