Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Interesting problem
Message
 
 
À
29/10/2008 10:07:16
Moacyr Zalcman
Independent Consultant
São Paulo, Brésil
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Autre
Versions des environnements
SQL Server:
SQL Server 2005
Divers
Thread ID:
01357902
Message ID:
01358068
Vues:
17
Hi Moacyr,

I think the question was more of if it is possible without extra tables or code. Also I believe I saw another way to detect what action initiated the trigger. And there is also another way to get current user ID as well.

But thanks a lot again.

>Hi, Naomi
>
>I use the following: I create a database AUDIT that has the same tables the one you want to audit , and some extra fields in each table : type of change, date of change and who changed. With a trigger like the above you can select the action to audit and if you only want to audit for a certain period of time you can disable the trigger.
>
>CREATE TRIGGER [dbo].[TRG_audit_produto]
>ON [dbo].[produto]
>FOR DELETE,UPDATE
>AS
>
>DECLARE @ACT CHAR(6)
>DECLARE @DEL BIT
>DECLARE @INS BIT
>DECLARE @SQLSTRING VARCHAR(2000)
>
>SET @DEL = 0
>SET @INS = 0
>
>IF EXISTS (SELECT TOP 1 1 FROM DELETED) SET @DEL=1
>IF EXISTS (SELECT TOP 1 1 FROM INSERTED) SET @INS = 1
>
>IF @INS = 1 AND @DEL = 1 SET @ACT = 'UPDATE'
>IF @INS = 1 AND @DEL = 0 SET @ACT = 'INSERT'
>IF @DEL = 1 AND @INS = 0 SET @ACT = 'DELETE'
>
>IF @INS = 0 AND @DEL = 0 RETURN
>
>IF @ACT = 'INSERT' INSERT [AUDITL4].[DBO].[AUDIT_PRODUTO] SELECT *,'INSERT' ,GETDATE(),HOST_NAME() FROM INSERTED
>IF @ACT = 'DELETE' INSERT [AUDITL4].[DBO].[AUDIT_PRODUTO] SELECT *,'DELETE' ,GETDATE(),HOST_NAME() FROM DELETED
>IF @ACT = 'UPDATE' INSERT [AUDITL4].[DBO].[AUDIT_PRODUTO] SELECT *,'UPDATE' ,GETDATE(),HOST_NAME() FROM DELETED
>
>Hope it helps
If it's not broken, fix it until it is.


My Blog
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform