The idea is to take advantage of the CONTEXT_INFO statement. CONTEXT_INFO is a statement that returns the context_info value that was set for the current session or batch.
Here’s the remarks excerpt from the BOL:
Multiple active result sets (MARS) enables applications to run multiple batches, or requests, at the same time on the same connection. When one of the batches on a MARS connection runs SET CONTEXT_INFO, the new context value is returned by the CONTEXT_INFO function when it is run in the same batch as the SET statement. The new value is not returned by the CONTEXT_INFO function run in one or more of the other batches on the connection, unless they started after the batch that ran the SET statement completed.To illustrate, consider this table:
create table students ( StudentId tinyint identity(1,1), StudentName varchar(25) ) insert into students (studentname) select 'John' union all select 'Paul' union all select 'George' select * from studentsThis is how the table looks like:
StudentId StudentName --------- ------------------------- 1 John 2 Paul 3 GeorgeNow let's create the trigger..
create trigger trgDelete on students for delete as begin insert into students (studentname) values (cast(CONTEXT_INFO() as varchar(10))) endHere's the sample code to illustrate...
declare @namevar varchar(10) DECLARE @BinVar varbinary(128) set @namevar = 'Ringo' SET @BinVar = CAST(@namevar AS varbinary(128) ) SET CONTEXT_INFO @BinVar delete from students where studentname = 'Paul' select * from studentsHere is the result after the delete statement...
StudentId StudentName --------- ------------------------- 1 John 4 Ringo 3 GeorgeThis is just a sample that shows how a string can be access inside a trigger. Other data type may also be stored, just don't forget that the value needs to be converted into a varbinary, then just convert it back as needed.
As always, comments are welcome...
~~ CK
No comments:
Post a Comment