【问题标题】:SQLAlchemy raw sql with list in where clause mssql pyodbcSQLAlchemy 原始 sql 与 where 子句中的列表 mssql pyodbc
【发布时间】:2022-04-01 18:59:43
【问题描述】:

我想将一个列表传递给我的原始 sql where 子句,但我不断收到此错误:

sqlalchemy.exc.DBAPIError: (pyodbc.Error) ('HY004', '[HY004] [Microsoft][ODBC SQL Server Driver]Invalid SQL data type (0) (SQLBindParameter)'
id = [1, 2, 3]
query = text("select * from table where col in :id")
conn.execute(query, {'id': tuple(id)})

这应该有效(我认为它们是 StackOverflow 上的解决方案)但可能不适用于 sqlserver?如何使其适用于 mssql?

【问题讨论】:

    标签: sql-server sqlalchemy pyodbc


    【解决方案1】:
    id = [1, 2, 3]
    query = text("select * from table where col in :id")    
    query = query.bindparams(bindparam('id', expanding=True))
    conn.execute(query, {'id': id})
    

    根据 OP @jole5646 对他自己问题的编辑。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多