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!

0

1 Answer 1

2

Use a multiline string:

stmt = """select a,
b,
.....
50 columns + where + group by ...
"""
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot @klashxx! If I understand you right it will take all my statement all together and consider it as one statement?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.