我在使用预准备语句查询 Sybase IQ 时遇到问题。当我将整个查询键入为文本然后在没有参数的情况下对其调用 PrepareStatement 时,查询工作正常。但是当我坚持一个参数时,即使我的 sql 是正确的,我也会得到错误。知道为什么吗?
此代码运行良好并运行我的查询:
errorquery<<"SELECT 1 as foobar \
, (SUM(1) over (partition by foobar) ) as myColumn \
FROM spgxCube.LPCache lpcache \
WHERE lpcache.CIG_OrigYear = 2001 ";
odbc::Connection* connQuery= SpgxDBConnectionPool::getInstance().getConnection("MyServer");
PreparedStatementPtr pPrepStatement(connQuery->prepareStatement(errorquery.str()));
pPrepStatement->executeQuery();
但这是完全相同的事情,只是我没有直接在代码中输入“2001”,而是使用参数插入它:
errorquery<<"SELECT 1 as foobar \
, (SUM(1) over (partition by foobar) ) as myColumn \
FROM spgxCube.LPCache lpcache \
WHERE lpcache.CIG_OrigYear = ? ";
odbc::Connection* connQuery = SpgxDBConnectionPool::getInstance().getConnection("MyServer");
PreparedStatementPtr pPrepStatement(connQuery->prepareStatement(errorquery.str()));
int intVal = 2001;
pPrepStatement->setInt(1, intVal);
pPrepStatement->executeQuery();
这会产生此错误: [Sybase][ODBC Driver][Adaptive Server Anywhere]Invalid expression near '(SUM(1) over(partition by foobar)) as myColumn'
知道如果第二个失败,为什么第一个会起作用吗?您是否不允许在插入的 sql 参数或类似的东西中使用“分区依据”?