Excel connect to SQL Server using a different windows user name

love to learn anything 0 Reputation points
2024-06-08T16:55:06.82+00:00

hello,

I am trying to connect to SQL Server in MS Excel using a different windows user credential other than current logged on windows user ID.

using SQL Server database user name is not an option for me.

Is there any function that can impersonate in Excel VBA? or a connection string that we can add windows user ID and password to pull data to Excel sheet?

Windows 10
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
10,944 questions
Excel
Excel
A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
1,604 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,067 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Taylor 49,871 Reputation points
    2024-06-08T17:24:35.0933333+00:00

    Your only 2 options in SQL is Windows auth (which always uses the current Windows user login) or SQL auth and they are not interchangeable. The correct solution here is to either use the actual WIndows user account or switch to SQL auth.

    The only workaround would be to impersonate the user within the code. You can refer to this old thread on how to do that. But there are some caveats that you strongly need to consider first.

    • To impersonate the user you need the username and password. Since this is being done inside an Excel file script you would either need to prompt the user for this information or hard code it. If you hard code the information then you're giving anyone access to this file credentials to the account effectively. The only reasonable security solution here is to prompt the user but now everybody using the file needs that information so it is not much more secure.
    • VBA in Office is becoming more and more deprecated in lieu of the newer Javascript programming model and JS won't let you do impersonation for obvious reasons. So your solution is going to be dependent on stuff that is deprecated.

    I would reevaluate why you need to use different credentials and resolve that issue instead. For example if the SQL in question needs to be accessed by many different users then set up some AD groups to contain the allowed users and then add the AD group (or even just the users) to SQL with the necessary rights. This is the most secure approach and allows you to "revoke" permissions for a user if needed.

    If the issue is one of licensing (you only have 1 user CAL for example) then this is actually circumventing the licensing rules so you should just pay for more licenses. Alternatively create an API.

    If the issue is that this is a legacy DB and you need access to it then consider creating an API that interacts with the database and then have Excel call the API instead. This eliminates the need for credentials and allows you to use the appropriate credentials in the API to talk to the database. Of course this mandates knowing how to write an API and then hosting it somewhere so it can be called.

    2 people found this answer helpful.

  2. Olaf Helper 42,386 Reputation points
    2024-06-10T05:42:11.7433333+00:00

    SQL Server doesn't allow/support impersonation of different Windows accounts for login.

    But you can use th command "runas" to start MS Excel with a different Windows account, see

    https://learn.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2012-r2-and-2012/cc771525(v=ws.11)

    0 comments No comments