我正在尝试使用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的相关文档here 而且
答案 0 :(得分:2)
知道了。问题是查询结尾处的分号。这在控制台中运行良好,但在使用DBAL运行时显然会提供ORA-00911: invalid character
。去图......
修正查询:
// 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);