0

我正在尝试使用 Doctrine DBAL 在 Oracle 数据库上执行以下查询。我之前执行过其他查询,所以这不是连接问题。问题源于试图逃避保留字Number。我需要选择一列作为Number以后的逻辑。查询的简化工作版本如下:

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);

PHP代码如下

// 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);

当我这样做时,我得到一个ORA-00911: invalid character错误。

我也尝试过先准备语句,然后将转义的列名绑定到查询:

// ...
$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);

但这给了我一个不同的错误:ORA-00923: FROM keyword not found where expected.

我说的ORA-00923时候也发现了错误$ESCAPEDNUMBER = addslashes('"Number"');

我不知道问题是什么。有任何想法吗?

我的问题再次是如何使用 Doctrine DBAL 在 php 查询中正确转义 oracle 保留字。我使用引号是因为在 Oracle 中,查询中需要引号,否则你会得到一个ORA-00923: FROM keyword not found where expected" error.

可以在此处找到有关 DBAL的相关文档

4

1 回答 1

2

知道了。问题是查询末尾的分号。这在控制台中运行良好,但显然给出了ORA-00911: invalid character使用 DBAL 运行的时间。去搞清楚...

固定查询:

// 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);
于 2014-01-07T18:32:14.297 回答