1

I'm facing problems with sending multiple queries to SQL Server 2012 through pyODBC in Python. I have a DataFrame with queries and I want to use it to query DB. Something like this:

cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=mySERVER;DATABASE=myDB;UID=myUID;PWD=myPSWD')
cursor = cnxn.cursor()
cursor2 = cnxn.cursor()

for i in range(len(myDataFrame.Column_w_Queries)):
    query = '"' + myDataFrame.Column_w_Queries[i] + '"'
    cursor.execute(query)
    one = cursor.fetchone()
    print(one)

query in this example is "select * from [DB].[schema].[table1]" (including quotes).

Problem is, that when I run cursor.execute(query) I got following error:

ProgrammingError: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Could not find stored procedure 'select * from [DB].[schema].[table1]'. (2812) (SQLExecDirectW)")

What am I doing wrong?

1
  • Have you tried the query without the surrounding quotes? Commented Jun 1, 2015 at 13:15

1 Answer 1

1

You have to get rid of the surrounding double quotes. For example, open up SQL Server Management Studio, and try to run:

"select * from [DB].[schema].[table1]"

You'll get the error:

Msg 2812, Level 16, State 62, Line 1
Could not find stored procedure 'select * from [DB].[schema].[table1]'.

Now try:

select * from [DB].[schema].[table1]

...and it should work. Best of luck!

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. I would swear I was playing with quotes, but your answer help me a lot ;-)

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.