Closed isaac-florence closed 2 years ago
I'm not seeing the behaviour you describe. This works fine for me:
crsr.execute(r"""
SELECT COUNT(*) AS n FROM [GORD-HP\SQLEXPRESS].[mydb].[dbo].[million_rows]
""")
print(crsr.fetchval()) # 1000000
Thank you for having a look @gordthompson. I will continue to investigate and report back what the issue was. I suspect it is an error on my side.
Please first make sure you have looked at:
Environment
Issue
When trying to execute a cross server query in MS SQL Server, one uses a four part object specification (server.database.schema.table). If the server name has a backslash in it (e.g. 'servername\server'), pyodbc escapes the backslash by adding another backslash. This means the query returns an error ('server: "servername\server" not found in sys.servers').
I am relatively new to Python, coming from R where the odbc package doesn't escape backslashes making this possible in the equivalent of
cursor.execute(query)
.Is there a way to prevent pyodbc taking
SELECT * FROM [servername\server].[mydb].[dbo].[tablename]
and then executingSELECT * FROM [servername\\server].[mydb].[dbo].[tablename]
? I hope I have missed something!Many thanks for your help in advance