tags:

views:

136

answers:

3

I have a connection to a Microsoft SQL Server and want the change the connection authenticated user. Is it possible to do it without closing and reopening the connection?

The ideal is something like Oracle set role feature.

I'd love if the solution also works for SQL Server 2000.

+1  A: 

As far as I know SQL Server is very distinct on the account that is passed is the authenticated context. Take Enterprise Manager and other tools for example you must disconnect and re-connect to change users.

Plus looking at the way connection pooling works, it indicates that the connection itself is cached user specific, so if you changed executing parties part way through it would cause major problems with security.

So the short answer, no, it isn't possible as far as I know.

Mitchel Sellers
+1  A: 

You might want to take a look at app roles (sp_setapprole), but you must be aware of consequences being that once the context is changed (e.g. the role is set), it can't be reverted with SQL Server 2000 (it's possible with 2005). The result of this is that the connection is effectively useless when closed in your code, e.g. it can't be returned to the pool and reused, which leads to scalability issues.

Otherwise it is not possible to change the security context once it has been established.

liggett78
A: 

Depending on what you are doing, EXECUTE AS may help you out here. This allows you to execute SQL in the context of another user in a similar fashion to the RUN AS available from the Windows shell. The profiler and audit tracing in SQL Server allows you to see both the original user and which context a statement is run under.

EXECUTE AS USER = 'newuser'; SELECT ... <-- SQL code from under the context of newuser REVERT;

Note: This is not available under SQL Server 2000 and was added because of requests like yours.

Marcus Erickson