Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8779977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:55:49+00:00 2026-06-13T19:55:49+00:00

I am currently using the CONTEXT_INFO property of the Master database for storing the

  • 0

I am currently using the CONTEXT_INFO property of the Master database for storing the logged in username to use it later in Table Triggers for auditing.

While migrating to SQL Azure, the issue of Cross-Database connections popped and I couldn’t find direct solutions to this issue.

Following are the Issue Details:

  1. I call Stored Procedure XXX from Data Access Layer and pass the Username as Parameter
  2. The username is used to set the CONTEXT_INFO value in XXX
  3. The CONTEXT_INFO value is then used in Tables Insert/Update/Delete Triggers to Store Username for Application Auditing

Solutions that I found so far:

  1. Create Table In Database to work as CONTEXT_INFO
  2. Use 2 Connection Strings in Data Access Layer, one for Master Database (to set CONTEXT_INFO) and the other is for the application and execute the SET CONTEXT_INFO each time before opening the connection to my application

But I find both solutions risky, specially when expanding the Database over multiple SQL Azure Databases in the future.

Appreciate your support.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T19:55:50+00:00Added an answer on June 13, 2026 at 7:55 pm

    The approach I took is shown below. On trick was to check to see running not running on SQL Azure, then we would need to call ‘SET CONTEXT_INFO …’. This allows the same code to be execute on local SQL Server Express and Azure without changes.

    1. Create a table to store the context info (not in master but in the same database)

      CREATE TABLE [dbo].[ContextInfo] (
          [ContextInfo] varbinary(128) not null,
          [ApplicationUsername] nvarchar(128) not null,
          [UpdatedAt] datetime NOT NULL,
          CONSTRAINT [PK_UserContextInfo] PRIMARY KEY CLUSTERED ([ContextInfo] ASC)
      )
      
    2. Create a stored procedure to ‘Set Context Info’ which is called from application

      CREATE PROCEDURE [dbo].[SetContextInfo]
        @ApplicationUsername nvarchar(128)
      AS
      
      SET NOCOUNT ON
      
      -- Remove all context items older than an 5 minutes ago
      DELETE
        FROM [dbo].[ContextInfo]
       WHERE [UpdatedAt] < DATEADD(mi, -5, GETUTCDATE())
      
      --
      -- Use the MERGE command to do an update/insert
      -- See: http://technet.microsoft.com/en-us/library/bb510625.aspx
      --
      
      IF SERVERPROPERTY('edition') <> 'SQL Azure'
      BEGIN
          DECLARE @b varbinary(128)
          SET @b = CONVERT(varbinary(128),newid())
          EXEC sp_executesql @statement=N'SET CONTEXT_INFO @b',@params=N'@b varbinary(128)',@b=@b
      END
      
      DECLARE @ContextInfo varbinary(128)
      SELECT @ContextInfo = CONTEXT_INFO()
      
      MERGE [dbo].[ContextInfo] AS target
      USING (SELECT @ContextInfo, @ApplicationUsername) AS source ([ContextInfo], [ApplicationUsername])
         ON (target.[ContextInfo] = source.[ContextInfo])
       WHEN MATCHED THEN 
              UPDATE SET [ApplicationUsername] = source.[ApplicationUsername], [UpdatedAt] = GETUTCDATE()
       WHEN NOT MATCHED THEN  
              INSERT ([ContextInfo], [ApplicationUsername], [UpdatedAt])
              VALUES (source.[ContextInfo], source.[ApplicationUsername], GETUTCDATE());
      
    3. Create a stored procedure to ‘Get Context Info’

      CREATE PROCEDURE [dbo].[GetContextInfo]
      AS
          SET NOCOUNT ON
          DECLARE @ContextInfo varbinary(128)
          SELECT @ContextInfo = CONTEXT_INFO()
      
          SELECT [ApplicationUsername]
            FROM [dbo].[ContextInfo]
           WHERE [ContextInfo] = @ContextInfo
      GO
      
    4. In trigger source, use:

      DECLARE @UserContext TABLE ([Username] VARCHAR(128))
      INSERT INTO @UserContext (Username)
      EXEC [dbo].[GetContextInfo]
      

    Now you have the username stored in the table variable. In case changes are applied by an administrator outside of your application, you may also want to check if the username was not set and default to something like *SYSTEM_USER*.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently using asp.NET MVC to build a Content Management System parts of
I'm currently developing a video player for streamed-content using the AVPlayer Framework. I stumbled
Currently I am using Canvas2Image to save the content of my HTML5 canvas. It
Currently I'm using $str = preg_replace(/\([^\)]+\)/, , $str); // Remove () and Content $str
Currently using the HTTPServletRequest class and specifically the .getQueryString method to retrieve an inputted
Currently using Google Analytics as a supplement to our paid tracking software, but neither
Currently using MySQL version 5.1.6 This is my first real world build and so
Currently using Xcode 4.2 and I have two view controllers (1 and 2). I
I currently using android NDK to write some native code in C. I have
I'm currently using the NEST ElasticSearch C# Library for interacting with ElasticSearch. My project

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.