How to pass entire WHERE statement from Python to SQL Server

hsvhsicv  于 2023-10-15  发布在  Python
关注(0)|答案(1)|浏览(112)

The following code returns error:
pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near '@P1'. (102) (SQLExecDirectW)")

Python code is:

c, conn = connection()
 query = "SELECT case_Case_Number \
     FROM tbl_case \
     ?"
 c.execute(query, (workflow_eval_statement, ))

The workflow_eval_statement is:

WHERE case_Case_Number = '23-00000500' and case_Case_Status = 'In Process'   and case_Case_Source = 'Portal'

When I use that WHERE statement via a query directly into SQL Server to returns that record correctly, without error.

If I modify the program to hard code the field names and pass the values it works:

query = "SELECT case_Case_Number \
    FROM tbl_case \
    WHERE case_Case_Number = ? and case_Case_Status = ? and case_Case_Source = ?"
c.execute(query, (parm_case_id, parm_status, parm_source))

Results in no error

I also tried passing the pre-built WHERE clause with double quotes (see below) and get the same error.

WHERE case_Case_Number = "23-00000500" and case_Case_Status = "In Process"   and case_Case_Source = "Portal"

Any guidance is appreciated.

1mrurvl1

1mrurvl11#

After reading the docs, this is what I found.

According to the pyodbc docs for execute:
The optional parameters may be passed as a sequence, as specified by the DB API, or as individual values.

And then, according to the DP API docs fr execute:

Parameters may be provided as sequence or mapping and will be bound to variables in the operation. Variables are specified in a database-specific notation

And then:

The module will use the getitem method of the parameters object to map either positions (integers) or names (strings) to parameter values. This allows for both sequences and mappings to be used as input

So I guess is not possible to pass a WHERE statement as a parameter but it's values.

相关问题