Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

/* Q3. Assuming a user is already connected to a SQL Server using a login named

ID: 3713713 • Letter: #

Question

/* Q3.
Assuming a user is already connected to a SQL Server using a login named 'Anna', what
statement does this user need to execute in order to switch the execution context from
'user1' to another, say, 'Jack', in order to run scripts or statements based on 'Jack'?
What statement is needed to switch the execution context back to 'Anna'?
*/

--To switch the execution context from login 'Anna' to another login 'Jack':

(insert code)

--To switch the execution context from 'Jack' back to 'Anna':

(insert code)

Explanation / Answer

Hi Dear,

Please find my answer.

Let user1 is already connected with Anna

CREATE USER user2 FOR LOGIN Jack;

GO

--setting permissions on user2 to user1

--so that user1 will set the execution context to user2.

GRANT IMPERSONATE ON USER:: user2 TO user1;

GO

--Test the execution context is now Anna.

SELECT SUSER_NAME(), USER_NAME();

-- Anna will transfers the execution context to Jack.

EXECUTE AS USER = 'user2';

--Display the present execution context.

SELECT SUSER_NAME(), USER_NAME();

--The REVERT statement will change the execution context back to Anna .

REVERT;

--Displaying the present execution context.

SELECT SUSER_NAME(), USER_NAME();