Question: How can I run SQL queries via Python to retrieve data from multiple servers that may have different SQL dialects (e.g. where one server dialect uses TOP X and another SAMPLE X)?
Currently I have some code that allows me to run a query against each server separately (which is insufficient):
import pyodbc
import pandas as pd
cnxn = pyodbc.connect('DSN=dsn_name;UID=username;PWD=password')
query = str("""
SELECT *
FROM sqlserver1.tableA
""")
df = pd.read_sql(query, cnxn)
Goal: What I would like to do is to join data from a different server, basically I would like to have an SQL query in Python that looks somewhat like this - handling different SQL dialects:
query = str("""
SELECT *
FROM sqlserver1.tableA as sq
INNER JOIN teradataserver2.tableB as tera
ON sq.id = tera.id
""")
.. P.S. I have tried a workaround like this:
import pyodbc
import pandas as pd
cnxnServer1 = pyodbc.connect('DSN=dsn_name1;UID=username;PWD=password')
cnxnServer2 = pyodbc.connect('DSN=dsn_name2;UID=username;PWD=password')
queryServer1 = str("""
SELECT *
FROM sqlserver1.tableA
""")
queryServer2 = str("""
SELECT *
FROM teradataserver2.tableB
""")
.. and then use a Pandas Merge. But this is too ineffective.
Note: I run Python 3.7