| Recently I have following situation: I don't need to execute trigger's code on some condition, like if it is called from particular stored procedure. If data is being updated from any other places, like application, or any other stored procedures, trigger code should be executed, but when one particular Stored Procedure is updating data to that table, it should not allow to do so. I search and I got the option CONTEXT_INFO with SQL SERVER. Let me share this with all of you. NOTE: We can use CONTEXT_INFO value in that session only. If there is new session we cannot use that value stored in CONTEXT_INFO. We can set CONTEXT_INFO up to 128 bytes of binary information with the current session or connection. Reference: http://msdn.microsoft.com/en-us/library/ms180125.aspx Let see an example, for better understanding: CREATE TABLE tblA( CREATE PROC TestA CREATE PROC TestB In this example, when we execute SP: TestA, it should allow to insert record in the table, while we execute SP: TestB, it should restrict. Here I set CONTEXT_INFO with SPName. I can use the same whatever is stored in CONTEXT_INFO during the current session. To make validation as defined above, lets create one trigger to restrict/allow user to proceed. CREATE TRIGGER trg_TblA In Trigger, as you can see, I again converted the SP name and compare it with CONTEXT_INFO. When Stored Procedure TestA is being executed, CONTEXT_INFO is not set, so trigger will not find comparison and allows user to insert record. While we execute TestB, as we have set CONTEXT_INFO, system will find comparison in trigger and restrict us to complete transaction. So system will give error message: So, by this way we can validate value in trigger, to make such decision |
This blog is for SQL SERVER developers. I am trying to publish the things that i face in my development career, so other developers can get help out of this BLOG.
July 31, 2009
SQL SERVER: Use CONTEXT_INFO
Labels:
CONTEXT_INFO,
DBA,
SQL,
SQL Server 2005,
SQL Tips,
Tejas Shah
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment