Why does sqlQuery from SAP HANA using RODBC return no data if request 18 or more rows

天涯浪子 提交于 2019-12-11 05:38:45

问题


I have a 64-bit Windows 7 machine with HANA Client installed and an ODBC connection to an SAP HANA database. In the ODBC Data Source administrator, I have added a connection to the required database and can successfully connect.

I am trying to use RStudio to retrieve data for analysis using R. I am finding that queries that return a handful of rows ("TOP 1" to "TOP 17") successfully return all 71 columns of data for the requested number of rows, but when I query with "TOP 18" or higher number of rows, I get all column titles, but 0 rows returned.

So the query:

res<-sqlQuery(ch, 'SELECT TOP 17 * FROM "SAPL2P"."/BIC/PZRPA_CNO" WHERE "/BIC/ZRPA_DCD"=\'CONFIRMED\'')

results in 17 rows of data, but

res2<-sqlQuery(ch, 'SELECT TOP 18 * FROM "SAPL2P"."/BIC/PZRPA_CNO" WHERE "/BIC/ZRPA_DCD"=\'CONFIRMED\'')

has 0 rows of data.

Any ideas what could be causing data not to be returned for more than 17 rows?


回答1:


Ok, the problem here really is how R on Windows handles UTF data from ODBC (as already has been described). A quick search around SO shows that this problem is pretty common for R on Windows with a lot of different DBMS.

For SAP HANA what worked for me was to add the following parameter to the ODBC DSN (in the ODBC driver settings -> Settings... -> Special property settings):

CHAR_AS_UTF8 | TRUE

This makes SAP HANA ODBC to handle SQL_C_CHAR as UTF8.




回答2:


I had a similar issue. At first I had no rows returns at all. Adding believeNRows=FALSE and rows_at_time=1 to the initial connection helped in at least bringing back some data (until a failure occurred). In my case I could only retrieve 5 rows with the 6th one failing which helped me to identify the problem row in the database.

In the end I believe it has to do with encoding as per your suggestion above. I found that my issue was with the inverted single quote character.

I found the following information on the RODBC cran site:

The other internationalization issue is the character encoding used. When R and the DBMS are running on the same machine this is unlikely to be an issue, and in many cases the ODBC driver has some options to translate character sets. SQL is an ANSI (US) standard, and DBMSs tended to assume that character data was ASCII or perhaps currently this is stymied by bugs in the ODBC driver, so SQLColumns is unable to report on tables in specified databases. More recently DBMSs have started to (optionally or by default) to store data in Unicode, which unfortunately means UCS-2 on Windows and UTF-8 elsewhere. So cross OS solutions are not guaranteed to work, but most do.

SAP HANA is a database that store data in Unicode.

I tried various options to set the encoding:

  1. setting the DBMSencoding property in the RODBS.odbcConnect proc to UTF-8, latin1, ISO8859-1 and UCS-2 without any luck
  2. I also tried setting the encoding in RStudio itself without luck

In the end I settled on creating a view in SAP HANA to replace the problem character in SQL. replace(content,x,y) as content where x was the problem character and y the replacement.

From that point on RODBC could retrieve the data without problem.

Hope this helps




回答3:


The solution turned out to require 2 parts as the issue arose due to Baltic characters within the SAP data. The 2 steps were:

  • Adding the property 'CHAR_AS_UTF8' with a value of 'TRUE' in the ODBC DSN settings as Lars suggested above.
  • Opening the channel in RStudio with an extra parameter 'DBMSencoding="UTF-8"‌'​

So the RStudio command to open the channel to the HANA data is now: ch<-odbcConnect("HA‌​NA_QA_DS",uid="aaaaaa‌​aa",pwd="bbbbbbbb", DBMSencoding="UTF-8"‌​)

Thanks, Lars. Your input was instrumental to solving this!




回答4:


I had the same issue. Adding the property 'CHAR_AS_UTF8' = 1 was not an option for me, since it has to be defined on the system of every new user using the code.

In my solution I cast the column to NCLOB.

res<-sqlQuery(ch, 'SELECT TO_NCLOB("COLUMNNAME") as "COLUMNNAME" FROM "SAPL2P"."MYSCHEMA"')

This might be complicated on the selection of *. However, mostly only certain known columns are affected by undesired characters.



来源:https://stackoverflow.com/questions/38969669/why-does-sqlquery-from-sap-hana-using-rodbc-return-no-data-if-request-18-or-more

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!