1

Please guide me how can I pass a very big SQL statement (50 columns) to stmt variable I will use to load into pandas df afterwards?

Here is what I have done:

from sqlalchemy import create_engine, MetaData, Table
import pandas as pd
import pyodbc

engine = create_engine('mssql+pyodbc://uid:pwd@servername/DBName?driver=SQL+Server+Native+Client+11.0')
con = engine.connect()

stmt = select a,
b,
.....
50 columns + where + group by ...

rs = con.execute(stmt)

df = pd.DataFrame(rs.fetchmany(size = 50))
df.columns = rs.keys()

In other words how can I put this large sql statement in ' '? If I will pass it in one row it will be unreadable.

Please advise, thanks in advance!

1 Answers1

2

Use a multiline string:

stmt = """select a,
b,
.....
50 columns + where + group by ...
"""
Juan Diego Godoy Robles
  • 14,447
  • 2
  • 38
  • 52
  • Thanks a lot @klashxx! If I understand you right it will take all my statement all together and consider it as one statement? – Daniel Green Jun 27 '17 at 10:38