doukanhua0752 2014-01-06 22:23
浏览 49
已采纳

在使用Doctrine DBAL在Oracle数据库上执行查询时,如何逃避oracle保留关键字?

I'm trying to use Doctrine DBAL to execute the following query on an Oracle database. I've executed other queries before so it's not a connection issue. The problem stems from trying to escape the reserved word Number. I need to select a column as Number for later logic down the road. The simplified working version of the query is as follows:

SELECT instructor.SecNum AS "Number"
      FROM (
           SELECT dbA.ID,
                  dbA.ClsNumber as SecNum,
                  dbA.CrsCatlgNbr,
                  ROW_Number() OVER (PARTITION BY dbA.ClsNumber ORDER BY dbA.CrsCatlgNbr) AS rn
           FROM dbA
           WHERE (dbA.SubCd = '5')
           AND (dbA.ID IS NOT NULL)
           ) instructor,
           (
            SELECT dbB.SecNum
            FROM dbB
            WHERE (dbB.SubCd = '5')
           ) student
      WHERE (instructor.rn = 1)
      AND (instructor.SecNum = student.SecNum);

The PHP code is as follows

// set up connection stuff and relevant variables
$sectionTable = array();
$dss1prdDatabase->executeQuery(
"SELECT instructor.SecNum AS \"Number\"
      FROM (
           SELECT dbA.ID
                 dbA.ClsNumber as SecNum
                 dbA.CrsCatlgNbr
                 ROW_Number() OVER (PARTITION BY dbA.ClsNumber ORDER BY dbA   CrsCatlgNbr) AS rn
           FROM dbA
           WHERE (dbA.SubCd = '5')
           AND (dbA.ID IS NOT NULL)
           ) instructor,
           (
            SELECT dbB.SecNum
            FROM dbB
            WHERE (dbB.SubCd = '5')
           ) student
      WHERE (instructor.rn = 1)
      AND (instructor.SecNum = student.SecNum);", $sectionTable);

When I do this I get an ORA-00911: invalid character error.

I've also tried preparing the statement first and then binding the escaped column name to the query:

// ...
$ESCAPEDNUMBER = '\"Number\"';
$sql = "SELECT instructor.SecNum AS ?
      FROM (
           SELECT dbA.ID
                 dbA.ClsNumber as SecNum
                 dbA.CrsCatlgNbr
                 ROW_Number() OVER (PARTITION BY dbA.ClsNumber ORDER BY dbA   CrsCatlgNbr) AS rn
           FROM dbA
           WHERE (dbA.SubCd = '5')
           AND (dbA.ID IS NOT NULL)
           ) instructor,
           (
            SELECT dbB.SecNum
            FROM dbB
            WHERE (dbB.SubCd = '5')
           ) student
      WHERE (instructor.rn = 1)
      AND (instructor.SecNum = student.SecNum);"
$stmt = $dbalDatabaseConnection->prepare($sql);
$stmt->bindvalue(1, $ESCAPEDNUMBER);
$stmt->execute($sectionTable);

But this gives me a different error: ORA-00923: FROM keyword not found where expected.

The ORA-00923 error is also found when I say $ESCAPEDNUMBER = addslashes('"Number"');

I have no idea what the issue is. Any ideas?

Again my question is how to properly escape oracle reserved words in php queries using Doctrine DBAL. I use quotes because in Oracle quotes are required in the query otherwise you'll get a ORA-00923: FROM keyword not found where expected" error.

Relevant documentation on DBAL can be found here And also

  • 写回答

1条回答 默认 最新

  • dqfr90979 2014-01-07 18:32
    关注

    Got it. The issue was the semi-colon at the end of the query. This works fine in the console but apparently gives the ORA-00911: invalid character when ran with DBAL. Go figure...

    Fixed query:

    // set up connection stuff and relevant variables
    $sectionTable = array();
    $dss1prdDatabase->executeQuery(
    "SELECT instructor.SecNum AS \"Number\"
          FROM (
               SELECT dbA.ID
                     dbA.ClsNumber as SecNum
                     dbA.CrsCatlgNbr
                     ROW_Number() OVER (PARTITION BY dbA.ClsNumber ORDER BY dbA   CrsCatlgNbr) AS rn
               FROM dbA
               WHERE (dbA.SubCd = '5')
               AND (dbA.ID IS NOT NULL)
               ) instructor,
               (
                SELECT dbB.SecNum
                FROM dbB
                WHERE (dbB.SubCd = '5')
               ) student
          WHERE (instructor.rn = 1)
          AND (instructor.SecNum = student.SecNum)", $sectionTable);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100