0

I have a .sql script.

The script first checks function existence, then create a function if not exist.

It then declares variables.

Finally, it executes a query.

The script can produce correct result when executing in sql server management studio.

I used pandas to read the table:

with open('query.sql', 'r') as f:
    query = f.read()

df = pd.read_sql_query(query, conn_str)

But it returns

sqlalchemy.exc.ProgrammingError: (pyodbc.ProgrammingError) ('42000', '[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near \'GO\'. (102) (SQLExecDirectW); [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]\'CREATE FUNCTION\' must be the first statement in a query batch. (111); [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Must declare the scalar variable "@number". (137); [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]A RETURN statement with a return value cannot be used in this context. (178); [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near \'GO\'. (102)') 

What can I do?

2
  • have you checked your connection string Commented Mar 20, 2019 at 6:41
  • yes, it is correct. Commented Mar 20, 2019 at 7:06

1 Answer 1

1

use pd.read_sql

df = pd.read_sql("SELECT * FROM table_name ",connection_string)

so in your case

df = pd.read_sql(query, conn_str)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.