sisdog
sisdog

Reputation: 2719

Why does PyODBC Thinks my SQL Only has 1 Param

Can someone help me understand why ODBC is only thinking I have 1 parameter in my sql?

sql = """
   IF OBJECT_ID('?', 'U') IS NOT NULL
   begin
       DROP TABLE ?
   end"""
cursor.execute(sql, table_name, table_name)
pyodbc.ProgrammingError: ('The SQL contains 1 parameter markers, but 2 parameters were supplied', 'HY000')

Upvotes: 0

Views: 73

Answers (1)

David Browne - Microsoft
David Browne - Microsoft

Reputation: 89091

You're passing the literal string '?' to the OBJECT_ID function.

IF OBJECT_ID('?', 'U') IS NOT NULL

should be

IF OBJECT_ID(?, 'U') IS NOT NULL

But if this is SQL Server there's another problem. You can't parameterize DDL statements, and DROP TABLE is a DDL statement.

Upvotes: 1

Related Questions