2015-12-09 48 views
-1

我用R從Hana數據庫中讀取數據。某些表名包含反斜槓,這迫使我使用引號。我無法使用R來閱讀這些表格。讓我告訴你一個例子...用H查詢Hana數據庫中的數據,處理報價

這個SQL工作在漢娜:

SELECT COUNT(*) FROM P3O."/BBB/BBB"; 

當我嘗試使用相同的代碼從哈納數據庫R鍵讀取數據我得到這些錯誤:

library("RODBC") 
channel <- odbcConnect("xxx",uid="xxx",pwd="xxx") 
query <- 
paste("'","SELECT COUNT(*) FROM P30.", "\"/BBB/BBB\"","'",sep="") 
RAW_dataHana <- sqlQuery(channel, query) 
close(channel) 

我收到以下錯誤:

Syntax error or access violation;257 sql syntax error: incorrect syntax near \"SELECT COUNT() FROM ... [2] "[RODBC] ERROR: Could not SQLExecDirect ''SELECT COUNT() FROM P30.\"/BBB/BBB\"''"

我認爲這是與報價,但是當我檢查代碼這一點,我覺得我得到了正確的查詢:

x = paste("'","SELECT COUNT(*) FROM P30.", "\"/BBB/BBB\"", "'",sep="") 
cat(x) 
> cat(x) 
'SELECT COUNT(*) FROM P30."/BBB/BBB"' 

回答

1

剛拿到現在來檢查我的RODBC測試代碼... 處理引用最簡單的方法就是用單查詢字符串報價,如:

sales_fact<-sqlQuery (ch, 'SELECT TOP 200 "ORDERID", "VAR_INDICATOR", 
             sum("ORDER_CNT") AS "ORDER_CNT", 
             sum("VARIANCE") AS "VARIANCE", 
             sum("VARIANCE_PCT") AS "VARIANCE_PCT", 
             sum("BUDGET") AS "BUDGET", 
             sum("ACTUAL") AS "ACTUAL" 
          FROM "_SYS_BIC"."test/ODERS_CV" 
          GROUP BY "ORDERID", "VAR_INDICATOR"') 

這也適用於膏狀():

queryText <- 'SELECT TOP 200 "ORDERID", "VAR_INDICATOR", 
             sum("ORDER_CNT") AS "ORDER_CNT",...' 

queryText <- paste(sep = '', queryText, ' "_SYS_BIC"."test/ODERS_CV" 
          GROUP BY "ORDERID", "VAR_INDICATOR"')