简体   繁体   中英

Not able to fetch all rows of data from MS SQL Server using RODBC even with believeNRows=F

I am trying to use RODBC library in R to fetech data from Microsoft SQL Server through a query, but the data I got is incomplete even if I set believeNRows=FALSE . The Microsoft SQL Server version is SQL Server 2016 SP1 CU3

The R code is as following:

library(RODBC)
sql.server = 'GDCSCTDDBSWA01'
database.name = 'Data.Analytics'
sql.string = 'select * from [Data.Analytics].[dbo].[Table]'
db.string <- sprintf("driver={ODBC Driver 13 for SQL Server}; server=%s;database={%s}; trusted_connection=yes", server , database.name)
db.channel <- odbcDriverConnect(db.string, believeNRows=FALSE)
itin.data <- data.table(sqlQuery(db.channel, sql.string))
close(db.channel)

It only returned around 1500 rows of data (the exact number of rows changes in each run, but it is around the same magnitude). However, when I ran the query in Microsoft SQL Server Management Studio, it worked correctly.

To eliminate the possibility of network issue, I also tried pyodbc and it also worked fine. The python code is as following:

import pyodbc
connection= pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER=GDCSCTDDBSWA01;DATABASE={Data.Analytics};trusted_connection=yes')
cursor = connection.cursor()
sql = 'select * from [Data.Analytics].[dbo].[Table]'
cursor.execute(sql)
dataList = cursor.fetchall()
connection.close()

Does anyone have an idea what causes RODBC to fail?

Setting believeNRows = FALSE in the sqlQuery statement should pull all rows.

sqlQuery(myconn, "select * from table", believeNRows = FALSE)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM