0

I am attempting to run a Python script that queries from multiple databases in SQL Server Management Studio via pyodbc. However, when I run the script I get the following error

pyodbc.InterfaceError: ('28000', "[28000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'DOMAIN\\myusername'. (18456) (SQLDriverConnect); [28000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'DOMAIN\\myusername'. (18456)")

However, when I try and query the same Databases via SSMS, I can do so without an issue, so I'm frankly quite confused as to what's going on. Here's the relevant function from the Python script.

def get_999_connection(database: str):
# global dbconn_999
global dbenv
server = f'{dbenv}_999.sql.domain.org\{dbenv}_999'
print('[get_999_connection]')
# if dbconn_999 is None:
print('[get_999_connection] getting new connection')
dbconn_999_ind = pyodbc.connect(driver='{SQL Server Native Client 11.0}', server=server, database=database, trusted_connection='yes')

return dbconn_999_ind

Of course more than happy to provide any additional context or code snippets as required. Thanks so much!

  • Why are you using that driver, which is deprecated (see [SQL Server Native Client](https://learn.microsoft.com/en-us/sql/relational-databases/native-client/sql-server-native-client?view=sql-server-ver15)), and not Microsoft's ODBC Driver for SQL Server (see [Python SQL Driver - pyodbc](https://learn.microsoft.com/en-us/sql/connect/python/pyodbc/python-sql-driver-pyodbc?view=sql-server-ver15))? – Thom A Apr 08 '22 at 16:52
  • Related: https://stackoverflow.com/a/37702329/2144390 – Gord Thompson Apr 08 '22 at 17:42
  • No specific reason regarding the driver @Larnu , but just changed and updated with the new driver. – A. Bernardi Apr 08 '22 at 18:48
  • Thanks @GordThompson , do you think using the RUNAS command might be worthwhile, given others on my team are able to run the script but I cannot without getting this error? – A. Bernardi Apr 08 '22 at 18:49
  • The client-side error messages are intentionally vague so as to avoid helping unauthorized users. Since you can connect from SSMS... inspect the instance's ERRORLOG, look for the error 18456 messages related to your login, then use the State number in those error messages to determine the actual problem at [MSSQLSERVER_18456](https://learn.microsoft.com/en-us/sql/relational-databases/errors-events/mssqlserver-18456-database-engine-error). – AlwaysLearning Apr 08 '22 at 22:40

0 Answers0