Must declare the scalar variable @P160R

CEO 136 Reputation points
2021-09-13T18:31:05.813+00:00

I noticed sometimes, when I try to retrieve data using the sql format of:

self.cursor.execute('SELECT FROM tablename WHERE column1 LIKE ? OR column2 LIKE ? OR column3 LIKE ?' , (searchbox, searchbox, searchbox) )

It usually gives me an error message: ODBC SQL server (SQL Server) Must declare the scalar variable "P160R". [SQL Server statement could not be prepared]

What sort of shit is this?

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,116 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,582 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Tom Phillips 17,716 Reputation points
    2021-09-13T18:53:31.14+00:00

    cursor.execute is a Python command. This is a Microsoft SQL Server forum, not a Python forum. Please post your question on a Python forum.


  2. MelissaMa-MSFT 24,176 Reputation points
    2021-09-14T03:19:18.18+00:00

    Hi @CEO ,

    It seems that you use Python to retrieve data .

    Please have a try like below, replace apostrophe with double quotes and make three searchboxes different.

    cursor.execute("SELECT FROM tablename WHERE column1 LIKE ? OR column2 LIKE ? OR column3 LIKE ?" , (searchbox1, searchbox2, searchbox3) )  
    

    ODBC SQL server (SQL Server) Must declare the scalar variable "P160R". [SQL Server statement could not be prepared]

    You could find out where is "P160R" from. It is from one procedure,function or other.

    Then you could troubleshoot the relationship between the select statement and variable "P160R".

    You could also use a new verison of ODBC driver and have another try.

    Please provide more details then we could check further.

    Thank you for understanding!

    Best regards,
    Melissa


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. Tom Phillips 17,716 Reputation points
    2021-09-14T14:51:04.227+00:00

    My guess is one of the values you are passing is "P160R", and you probably need something like:

    self.cursor.execute('SELECT FROM tablename WHERE column1 LIKE ''?'' OR column2 LIKE ''?'' OR column3 LIKE ''?''' , (searchbox, searchbox, searchbox) )