Skip to content
Friday, May 9, 2025
Latest:
  • Changing Service tiers? Do you need downtime?
  • Different Database Services in AWS
  • Google Launched second Cloud Region in India
  • SQL Server IaaS Agent Extension |Part-1
  • Day-3 | SQL Server Internals and SQL Azure Workshop | IBM Noida

SQLChamp

Let's learn together!

  • Home
  • LearnSQL
  • SQLServer
  • SQLTips
  • Azure Data Platform
    • SQLAzure
    • SQLServer on Azure VM
  • Best Practices
  • Speaking Events
    • In-Person Events
    • Events
  • Contact – us
    • form7
LearnSQL

What do you mean by Execution Context & Context Switching? Part-1

April 4, 2013 Sarabpreet Singh Anand

Execution context is maintained by the login that is connected or executing the query, then it checks the permissions of the login against the currently requested query whether the login has the required permissions to perform this action or not.

In SQLServer we can easily change the execution context to any login by executing the EXECUTE AS statement\clause. Once the execution context is switched the remaining sessions (queries) will run under the execution context of another user (as if another user is executing that piece of code) until the execution context is explicitly reverted.

We can represent the Execution Context with a pair of security tokens, which are Login & User token. The tokens help in identifying the primary and secondary principals for which the permissions will be checked. A login connecting to an instance of SQL Server has one login token and one (or more) user tokens depending on the number of databases to which the login has access.

What all information resides in a Token?

A security token whether it is Login or User contains the below mentioned information:

1. One server or database principal as the primary identity

2. One or more principals as secondary identities

3. Zero or more authenticators

4. The privileges and permissions of the primary and secondary identities

Authenticators are principals, certificates, or asymmetric keys that vouch for the authenticity of the token. Frequently, the authenticator of a token is the instance of SQL Server.

A login token is valid across the instance of SQL Server. It contains the primary and secondary identities against which server-level permissions and any database-level permissions associated with these identities are checked. A Primary Identity is a login itself whereas a Secondary Identity is referred to the permissions inherited from Roles or Groups. Because Users are created at Database level Scope a user tokens do not contain any server-role memberships or permissions.

To visualize what exactly goes in a Token, let’s execute couple of queries.

SELECT * FROM sys.login_token;

GO

SELECT * FROM Sys.User_Token;

clip_image002.jpg

If Sarabpreet login were a member of other server-level roles, they would also be listed as secondary identities.

Point to Note: Members of the sysadmin fixed server role always have dbo as the primary identity of their user token.

In next parts we’ll learn How to actually perform Execution Context Switching and what all options are available to do so.

Hope you enjoyed the post, feel free to leave a comment. 🙂

Thanks,
Sarabpreet Singh Anand
Subscribe now to get latest Tips\blog posts in your Inbox
Follow @SQLDB

  • What do you mean by Principals? (with respect to SQL Server)
  • What do you mean by Execution Context & Context Switching? Part-2
  • Azure (2)
  • Azure Data Platform (32)
  • Azure Data Platform (2)
  • Best Practices (37)
  • Events (9)
  • In-Person (9)
  • In-Person Events (22)
  • Latest Announcements (6)
  • LearnSQL (32)
  • Powershell for SQL (5)
  • Powershell-Basics (1)
  • Pro-Tip (1)
  • Product Reviews (1)
  • Speaking Events (18)
  • SQL for Beginners (5)
  • SQL Server (1)
  • SQLAzure (33)
  • SQLServer (15)
  • SQLServer on Azure VM (17)
  • SQLServer on Azure VM (1)
  • SQLTips (22)
  • Uncategorized (9)
  • Webcast (6)

Always Encrypted (6) azure (32) azure portal (6) Azure SQL Database (10) backup (5) backup encryption (6) cloud (6) Database (4) DDM (5) deadlock (4) Dynamic Data Masking (5) error log (3) event (21) Execute As (4) Execution context (3) free (10) Free event (8) free sql learning (6) Function (3) grant (3) gurgaon (9) in person (9) learn (3) Learning (9) limitations (4) MCT (8) Microsoft (8) MVP (8) permissions (3) powershell (8) Powershell4SQL (3) restore (3) sarab (19) sarabpreet singh anand (4) security (11) SQL (51) SQL 2012 (5) SQL 2014 (8) SQL Azure (12) SQLAzure (10) SQL Database (10) SQL Saturday (9) SQLServer (31) sql server (17) sql server 2012 (6) SQLServer 2014 (9) sql server 2014 (4) SQL Server 2016 (8) SQLServerday (13) SQL Server day (9) SQLServer Workshop (3) sql tips (4) step-by-step (4) t-sql (8) tech days (3) tips (4) Training (10) tricks (3) webcast (7) windows azure (6)

Copyright © 2025 SQLChamp. All rights reserved.
Theme: ColorMag by ThemeGrill. Powered by WordPress.