2014-01-06 69 views
0

我正在嘗試使用Doctrine DBAL在Oracle數據庫上執行以下查詢。我之前執行過其他查詢,所以它不是連接問題。問題源於試圖逃脫保留字Number。我需要選擇一個列作爲Number,以供以後的邏輯使用。查詢的簡化工作版本如下:如何使用Doctrine DBAL在Oracle數據庫上執行查詢時如何轉義oracle保留關鍵字?

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保留字。我使用引號,因爲在甲骨文的報價查詢需要,否則你會得到一個DBAL ORA-00923: FROM keyword not found where expected" error.

相關文檔,可以發現here 而且還

+0

這是MySQL的事情,我不知道是否它轉換到Oracle,但你嘗試添加'\''繞柱的名字嗎?例如'\'dbA.ClsNumber \''編輯:看起來像[Oracle方法將使用雙引號](http://stackoverflow.com/questions/1162381/how-do-i-escape-a-reserved-word -in-oracle) – sjagr

+0

是的,Oracle使用雙引號,這就是爲什麼我一直在逃避引號等等。 – thed0ctor

+0

這看起來不像有效的SQL - 第二個選擇列表缺少逗號。 –

回答

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); 
相關問題