I am trying import tables in SQL Server 2017 into R. So, I wrote down these codes. Connection seems okay.
But when I import tables I get an error and a warning message like below. I can solve this problem by write sqlQuery or dbGetQuery in R but I would like to understand what is problem with dbReadTable. Finally I kindly ask you help me to list tables within particular schema in R.
install.packages("RODBC")
library(DBI)
library(RODBC)
install.packages("odbc")
library(odbc)
# Making proper conection.
con <- DBI::dbConnect(odbc::odbc(),
Driver = "SQL Server",
Server = "WINDOWS-M6QDHVB\\SQLEXPRESS",
Database = "AdventureWorks2017",
Trusted_Connection = "True")
#Import data wihtin the table into R.
dbReadTable(con, SQL("HumanResources.Department"))
dbReadTable(con, SQL("HumanResources.Employee"))
Warning message:
In dbClearResult(rs) : Result already cleared
Error in result_fetch(res#ptr, n) :
nanodbc/nanodbc.cpp:3186: 07009: [Microsoft][ODBC SQL Server Driver]Invalid Descriptor Index
Related
If I want to connect R to a database, do I still need to install driver for R user?
I had done successful connection with server but unable to read/write/fetch the tables.
library(odbc)
con <- dbConnect(odbc::odbc(),
.connection_string = 'driver={SQL Server};server=DW01;database=AF_DW;trusted_connection=true')
Now I can see AF_DW in RStudio connections.
dbListFields(con, "Visits")
I can see all the variables in the table "Visits"
data <- dbReadTable(con, "Visits")
Got an Error: nanodbc/nanodbc.cpp:1655: 42000: [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name 'Visits'. [Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be prepared. 'SELECT * FROM "Visits")
data3 <- dbGetQuery(con, "SELECT * FROM Visits")
Got same error
data4 <- dbSendQuery(con, "SELECT * FROM Visits")
Got same error
con_in_R <- dbFetch(dbSendQuery(con,"SELECT * FROM Visits"))
Error in (function (cond) : error in evaluating the argument 'res' in selecting a method for function 'dbFetch': nanodbc/nanodbc.cpp:1655: 42000: [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name 'Visits'. [Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be prepared. 'SELECT * FROM Visits'
Appreciate your help. Thank you.
According to you image, Visits is not stored in the default dbo schema as all your queries assume but under the eCW schema.
Like most RDBMS's, SQL Server follows the three part name convention for objects (tables, stored procedures, functions): [database].[schema].[object] where database is not necessary for a database specific connection and schema not necessary for dbo default.
Therefore, you need to reference schema and table name in your attempted queries.
s <- Id(schema = "eCW", table = "Visits")
# READ FROM NON-DEFAULT SCHEMA TABLE
data3 <- dbReadTable(con, s)
data3 <- dbGetQuery(con, "SELECT * FROM [eCW].[Visits]")
# WRITE TO NON-DEFAULT SCHEMA TABLE
dbWriteTable(conn, s, mydataframe)
dbWriteTable(con, SQL("eCW.Visits"), mydataframe)
I am trying to query an SQL Server database deployed on Azure from RStudio.
I established a connection. I can see all of the tables and get a preview of the data from the RStudio GUI. However, when I try to query the data I get an error.
library(dplyr)
library(odbc)
con <- dbConnect(odbc(),
Driver = "SQL Server",
Server = "#.database.windows.net",
Database = "loremipsum",
UID = "loremipsum",
PWD = "loremipsum",
Port = 1433)
First query I tried:
users <- as.data.frame(sqlQuery(con, "SELECT * FROM AspNetUsers"))
The error I got:
Error in sqlQuery(con, "SELECT * FROM AspNetUsers") :
first argument is not an open RODBC channel
I also tried this query
result <- dbSendQuery(con, "SELECT * FROM AspNetUsers")
first_100 <- dbFetch(result, n = 100)
With the following error:
Error in result_fetch(res#ptr, n) :
nanodbc/nanodbc.cpp:2966: 07009: [Microsoft][ODBC SQL Server Driver]Invalid Descriptor Index
It strange because the connection must be valid, given that it gets access to the data through the GUI.
Any hints?
Congratulations you have solved the issue:
I eventually solved the issue by switching to another libary - RODBC. It seems that thats a bug in the way odbc handles character columns as point:R DBI ODBC error: nanodbc/nanodbc.cpp:3110: 07009: [Microsoft][ODBC Driver 13 for SQL Server]Invalid Descriptor Index
I post this as answer and this can be beneficial to other community members.
Can you anyone help me with how to connect to Teradata using DBI ODBC Package?
I use the code below,
con <- dbConnect( drv = dbDriver('Teradata'),
server=prodServer,
DBCName=prodDatabaseName,
uid=username,
pwd=password,
MechanismName = TD2)
but it throws the following error:
Error: Couldn't find driver Teradata. Looked in:
* global namespace
* in package called Teradata
* in package called RTeradata
Looks like Teradata ODBC driver, DBCName is a network name (which for other drivers would typically supplied as Server)
con <- dbConnect(odbc::odbc(),
Driver = DRIVER,
DBCName = SERVER,
Database = defaultDatabase,
UID = Sys.getenv("tera_user"),
PWD = Sys.getenv("tera_pass"))
After quite a looot of research, I finally found this in an answer by #Fred to another question:
R-Studio - connection to Teradata is not working
I can not connect to MSSQL using DBI package.
I am trying the way shown in package itself
m <- dbDriver("RODBC") # error
Error: could not find function "RODBC"
# open the connection using user, passsword, etc., as
# specified in the file \file{\$HOME/.my.cnf}
con <- dbConnect(m, dsn="data.source", uid="user", pwd="password"))
Any help appreciated. Thanks
As an update to this question: RStudio have since created the odbc package (or GitHub version here) that handles ODBC connections to a number of databases through DBI. For SQL Server you use:
con <- DBI::dbConnect(odbc::odbc(),
driver = "SQL Server",
server = <serverURL>,
database = <databasename>,
uid = <username>,
pwd = <passwd>)
You can also set a dsn or supply a connection string.
It looks like there used to be a RODBC driver for DBI, but not any more:
http://cran.r-project.org/src/contrib/Archive/DBI.RODBC/
A bit of tweaking has got this to install in a version 3 R but I don't have any ODBC sources to test it on. But m = dbDriver("RODBC") doesn't error.
> m = dbDriver("RODBC")
> m
<ODBCDriver:(29781)>
>
Suggest you ask on the R-sig-db mailing list to maybe find out what happened to this code and/or the author...
Solved.
I used library RODBC. It has great functionality to connect sql and run sql queries in R.
Loading Library:
library(RODBC)
# dbDriver is connection string with userID, database name, password etc.
dbhandle <- odbcDriverConnect(dbDriver)
Running Sql query
sqlQuery(channel=dbhandle, query)
Thats It.
I'm trying to retrieve data from greenplum cluster into R (win client).
I've tried:
library("RODBC")
conn <- odbcDriverConnect("DSN_name")
Sql <- "select * from DB.st.country"
cen_data <- sqlQuery(conn,Sql)
print(cen_data)
I'm getting error:
0A000 7 ERROR: cross-database references are not implemented
I have seen some answers about dblink but when I tried:
sql <- "select dblink_connect('conn', 'dbname=myDB');"
cen_data <- sqlQuery(conn,Sql)
I'm getting error:
"42883 7 ERROR: function dblink_connect(unknown, unknown) does not exist
Does anyone have any idea what Am I doing wrong?
Instead of ODBC, you can also use the RPostgreSQL package, which uses DBI as the backend.
drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv, host="hostname", user=..., pass=...)
This is not an R issue (syntax is ok).
The problem was in database definitions.
You need to have the database in the "select data source". for that you need to have postgreSQL.