SQLServer : Impersonating Users

by bitznbitez

When working on a permissions problem sometimes it can be handy to verify the problem yourself instead of relying on the end user directly.   The now depricated SETUSER command is very handy, assuming you have sysadmin level privilege.

setuser 'bogusUserName'
go
select top 10 * from dbo.whateverTable;
setuser
go

The last setuser will return you to your earlier privileged user.    If you don’t want to use the depricted method ( and you shouldn’t ) then you should use EXECUTE AS

execute as LOGIN = 'bogusUserName';
select top 10 * from dbo.whateverTable;
revert;

This can be very useful.

Advertisements