Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««123

Access variables values from Trigger Expand / Collapse
Author
Message
Posted Wednesday, June 9, 2010 9:23 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, March 8, 2012 1:46 AM
Points: 148, Visits: 31
providing you keep the number series separate ,
and can easily distinguish between them ,
and provided loads of people don't login using the same account,
and that these are likely to be true next week, month, year,..
Post #934688
Posted Wednesday, June 9, 2010 9:42 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, June 7, 2011 2:19 PM
Points: 4, Visits: 13
Why not just insert the row into the audit table first from SP directly and delete the actual table since you have all the needed info within SP itself? Using the trigger and context_info is complicating the code for no good reason that I can see based on the info in the article.

I'm a big fan of KIS (keep it simple) principal. If you can avoid the triggers in the first place, absolutely avoid them.
Post #934706
Posted Wednesday, June 9, 2010 9:55 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Today @ 8:24 AM
Points: 982, Visits: 753
minimyme (6/9/2010)

I'm a big fan of KIS (keep it simple) principal. If you can avoid the triggers in the first place, absolutely avoid them.


Auditing is normally performed in triggers

1) for security purposes so the action is audited however the action is performed and (pretty much) whoever it is performed by and

2) because the auditing logic is decoupled from the deletion logic.

Dave.
Post #934721
Posted Wednesday, June 9, 2010 10:19 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Saturday, July 26, 2014 1:08 AM
Points: 51, Visits: 180
Hi Mahesh,

Context_info is unique per session. So, when multiple users are working on product, system will create separate session for each session. So there is no any issue.

Thanks,
Tejas
SQLYoga.com
Post #934746
Posted Wednesday, June 9, 2010 10:26 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Saturday, July 26, 2014 1:08 AM
Points: 51, Visits: 180
Why not just insert the row into the audit table first from SP directly and delete the actual table since you have all the needed info within SP itself? Using the trigger and context_info is complicating the code for no good reason that I can see based on the info in the article.

I'm a big fan of KIS (keep it simple) principal. If you can avoid the triggers in the first place, absolutely avoid them.

Tejas:
In Application, it might happen that we allow the user to delete record from multiple pages OR sometimes some record will be deleted based on business condition. In any case, we need to make sure that we have audit data, who deleted and the reason?

To make sure that we have audit data, trigger is a place where we can write a code to keep audit data when any information of that table is being changed.

Thanks,

Tejas
SQLYoga.com

Post #934752
Posted Wednesday, June 9, 2010 10:53 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, June 16, 2014 9:38 AM
Points: 2,163, Visits: 2,189
Interesting article, thanks. I'm not sure I will end up using it, but I will keep it in the back of my mind for when an issue might come up.
Post #934774
Posted Wednesday, June 9, 2010 11:47 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, September 14, 2010 8:24 AM
Points: 69, Visits: 33
john_wong_ca (6/9/2010)
That's a good idea. This will work for most of cases but not for replication. If a table is published in a merge publication, while replication agent is modifying the data, the connection of the agent will use context info as part of the identification of merge agent. In this case, the context info passed in the trigger will not be xml conversable.


absolutely right
a solution that will work for replication is to add username and reason fields to the table, populate them with your delete proc then select them from DELETED in the trigger.

on the same topic, heres a cool slice I wrote to figure out what field changed to activate the trigger, in case you dont REALLY want to fire triggers generating history when a certain set of fields is updated, as in the case of the darned replication id... (intended to be inserted into the trigger whose action you want to modify based on the field(s) updated)


BEGIN TRANSACTION
declare @NameToCheck varchar(8)
set @NameToCheck = 'val3' --this is the column name to check for singleton update, set to ModifiedDate,

--if we need to check any combination of values, just expand this routine to compensate
declare @CheckSum int --holds the int value of two raised to (the ordinal position of this column minus one)
declare @ChangeSum int --holds the int value of the COLUMNS_UPDATED varbinary

--------------------------------------------------------------------------------------------------------------------------
--this is the guts of it, as you can see it is only really one line
--generate the checksum
select
@CheckSum = power(2,colorder-1) --colorder index is 1 based, switch to 0 based
from syscolumns where
id = (select id --this is the column id of the column we want to watch
from sysobjects where
name = (select object_name(parent_obj) --the name of the table to which this trigger is attached
from sysobjects where xtype = 'tr' --the trigger type label
and name = object_name(@@PROCID) --the name of this trigger))
AND name = @NameToCheck --is the column we are watching
--if more than one field needs checked, just do again and add the check sums together... or do some other clever thing...
---------------------------------------------------------------------------------------------------------------------------

--recover the change sum, the value of the bit mask
set @ChangeSum = cast(COLUMNS_UPDATED()as int)

--create meta label for the history record
declare @newmeta varchar(200)
set @newmeta = '@CheckSum=' + cast(@CheckSum as varchar(8)) + ' '
set @newmeta = @newmeta + '@ChangeSum=' + cast(@ChangeSum as varchar(8))

--here we check if our pattern of change occurred
if(@CheckSum = @ChangeSum)
begin
set @newmeta = @newmeta + ' update only in ' + @NameToCheck + ' where we watched'
goto createHistory
end

--otherwise
set @newmeta = @newmeta + ' some other update'

createHistory:
INSERT INTO jonathan1H
( val1, val2, val3, modDate, meta )
SELECT val1, val2, val3, getdate(), @newmeta
FROM deleted

COMMIT TRANSACTION
Post #934817
Posted Wednesday, June 9, 2010 8:20 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, June 26, 2014 7:37 AM
Points: 32, Visits: 181
In light of the other posts, this is just to clarify a comment you made in your article. You stated, "the first character is removed and then converted into an XML documnet." The subsequent code line was:
SELECT @XML = CAST(REPLACE(@VarXML,CHAR(0),'') AS XML)

However, the CHAR(0) does not replace the first character, but defines an ASCII 000 character, sometimes referred to as a NULL (not associated with a SQL NULL value) or end-of-document character.

Thus, this line of code is replacing an ASCII 0 with an empty string character (which is probably located at the end of the string, not the first character.
Post #935097
Posted Friday, June 11, 2010 2:19 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, July 27, 2010 4:58 AM
Points: 2, Visits: 7
Hello,
I think the example is not the luckiest as once you have a stored proc that deletes the record it could take care of the auditing part as well.
Adding auditing to a DB where DMLs commands are not wrapped into storedprocs is more tricky.

Áron-Hungary
Post #936296
« Prev Topic | Next Topic »

Add to briefcase «««123

Permissions Expand / Collapse