Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Problem with audit table trigger
Message
 
 
To
03/03/2014 17:35:35
General information
Forum:
Microsoft SQL Server
Category:
Scripting
Environment versions
SQL Server:
SQL Server 2012
Application:
Web
Miscellaneous
Thread ID:
01595410
Message ID:
01595706
Views:
35
>>>>>Thanks Naomi. I was able to make this work with one addition of a space after #del d (and I almost understand your changes). :)
>>>>>
>>>>You're welcome.
>>>
>>>Naomi -
>>>
>>>Here is a follow-up question. The trigger we've been talking about is working fine. One of the fields in the audit table is the name of the user who made the change. If the edit is made via SSMS, the name is logged OK. However most of the interaction is made via a web app where the user is connecting via a generic connection string and is known only as 'webuser'. If the user is logged in via the membership, is there a way the trigger can identify who he is? I suspect not, but I thought it was worth asking. How do others deal with this?
>>>Thanks
>>
>>No, unfortunately there is no way to get the actual user if the logging is done from the web using webuser.
>>
>>If the web application knows the user (say, operator), it can try providing this info using CONTEXT_INFO. Try googling on this topic.
>
>So something like this might work?
>
>oConn.Open()
>oCommand.Text = "DECLARE @ContextInfo varbinary(128),SELECT @ContextInfo = cast( @User AS varbinary(128) ),SET CONTEXT_INFO @ContextInfo"
>oCommand.Parameters.AddWithValue("@User", GetUser.Name) 
>oCommand.ExecuteNonQuery()
>oCommand.Text = "actual update command . . . . "
>oCommand.ExecuteNonQuery()
>oConn.Close()
>
>
>and then in the trigger we would set the user
>
>SELECT @UserName = cast( CONTEXT_INFO() as varchar(128) ) 
>
It might work. You need to use semicolon between commands in T-SQL and I also suggest to use Add instead of AddWithValue, but the idea is this. Also, I think this should be part of the whole UPDATE command, e.g. it should be the same batch.
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform