How to read an escaped table in SQL Server from R DBI? - r

I was able to use DBI::dbWriteTable to write a table with a non-standard name [name#place:funny/pages], but I am unable to read it back in with DBI::dbReadTable.
When I try with:
dbReadTable(con, '[name#place:funny/pages]')
I see the error:
Error: nanodbc/nanodbc.cpp:1655: 00000: [Microsoft][ODBC Driver 17 for SQL Server][SQL Serv [Microsoft][ODBC Driver 17 for SQL Server][S L Serv 'SELECT * FROM "[name#place:funny/pages]"'
sessionInfo tells me I am using odbc_1.3.2 and DBI_1.1.1.
What am I doing wrong? Is there a way around the problem? I need to use that naming scheme to maintain compatibility with established processes.
I am able to read this table without issue from SQL with:
select * from [name#place:funny/pages]

The answer is to not include brackets at all.

Related

Unable to query Azure SQL Server from RStudio

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.

MarkLogic ODBC for Tableau - Date casting error

I have a MarkLogic ODBC server for a tableau connection. I have created the view and is already accessible. However, I am having problems manipulating Date elements in Tableau. I've encountered the following error:
An error occurred while communicating with the Other Databases (ODBC) data source 'database (new.database) (__MarkLogic)'.
Bad Connection: Tableau could not connect to the data source.
ERROR: XDMP-CAST: (err:FORG0001) Invalid cast: () cast as xs:date;
Error while executing the query
SELECT SUM("database"."amount") AS "sum_amount_ok",
SUM("database"."offered") AS "sum_offered_ok",
"database"."type" AS "type"
FROM "new"."database" "database"
WHERE (CAST({fn TRUNCATE(EXTRACT(YEAR FROM CAST("database"."date_of_permit" AS DATE)),0)} AS INTEGER) = 2014)
GROUP BY 3
Running this query (removing the {fn} tag) in qconsole results in the same casting error. How do I resolve this?
Thanks!
Update:
As grtjn commented, there is a null entry in my data that is causing this casting error.

Error in executing R dbReadTable and dbGetQuery against odbc connection to OBIEE

I'm using Microsoft R Open version 3.5 and the most recent packages
for odbc and dbi as of December 19, 2018 downloaded via CRAN. I'm able to connect to the source, list tables, and confirm that the table exists per dbExistTable. The driver is an oracle driver that connects to an OBIEE database. I'm able to confirm that the driver, dsn, and query work independently from both Excel and SAS.
tbl_id <- DBI::Id(catalog="Human xxx", table="xxx")
dbExistsTable(con,tbl_id)
[1] TRUE
However I'm unable to read or query the tables, due to the error below.
dbReadTable(con,tbl_id)
Error: 'SELECT * FROM "Human xxx"."xxx"'
nanodbc/nanodbc.cpp:1581: S1092: [NQODBC] [SQL_STATE: S1092]
[nQSError: 10071] The option type is out of range.
Also get the same error with a query:
dbGetQuery(con,'SELECT Seg FROM "Account xxx"."Accounting xxx"')
Error: 'SELECT Seg FROM "Account xxx"."Accounting xxx"'
nanodbc/nanodbc.cpp:1581: S1092: [NQODBC] [SQL_STATE: S1092] [nQSError: 10071] The option type is out of range.
Where "Seg" is a simple text column of 10 characters.

Execute SQL with "like" statement in R Language

I am trying to execute a SQL Query through R to get the data from Access DB
Normal SQL statement works fine, but when it comes to like statement its throwing error
Below is code :
library(RODBC);
channel = odbcDriverConnect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:/Users/ADMIN/Documents/R.accdb")
test = sqlQuery(channel ,paste('SELECT R.ID, R.Template, R.WEDate FROM R WHERE R.Template Like "*slow*"'))
Error:
[1] "07002 -3010 [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 2."
[2] "[RODBC] ERROR: Could not SQLExecDirect 'SELECT R.ID, R.Template, R.WEDate FROM R WHERE (R.Template Like \"slow\")'
Is there a way to fix this.
Consider both of #joran's suggestions with single quote enclosing string literals AND using the ANSI-92 wildcard operator %. You would use asterisk, * (ANSI-89 mode) when running an internal query, namely inside the MSAccess.exe GUI program (which defaults to DAO) or if you connect externally to Access with DAO. Meanwhile, ADO connections uses the percent symbol which most external interfaces uses including RODBC.
I was able to reproduce your issue and both these remedies worked. Also, no need to use paste() as you are not concatenating any other object to query statement.
library(RODBC);
channel = odbcDriverConnect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};
DBQ=C:/Users/ADMIN/Documents/R.accdb")
test = sqlQuery(channel,
"SELECT R.ID, R.Template, R.WEDate FROM R WHERE R.Template Like '%slow%'")

SAS connection to Teradata Database using Teradata ODBC

I'm trying to connect to Teradata in SAS. I set up an teradata ODBC on the machine. The assumption currently for me is that using ODBC is the only way for me to access the database. And here is the syntax of my connection command:
Libname Teradata ODBC dsn = 'dsnname' uid = 'uid' pwd = 'pwd';
results:
Error: The ODBC engine cannot be found.
Error: Error in the LIBNAME statement.
It keeps saying that the ODBC engine cannot be found. I'm really confused now. Is there anything wrong with the command? Or I have to do something else outside SAS?
I check the licence
Proc Setinit;
result:
SAS/ACCESS Interface to Teradata ** the date shows not expired.
Could anyone give me some idea. Thank you very much!
Can't say I've ever used ODBC to access Teradata, can see it being highly inefficient.
Normally, you'd do pass-thru SQL to Teradata...
proc sql ;
connect to teradata (user='username' pass='password' tdpid=prodserver) ;
create table mydata as
select * from connection to teradata
(select a.*
from ds.enterprise_table as a) ;
disconnect from teradata ;
quit ;
For a direct libname, the syntax would be
libname tdata teradata user='username' pass='password' tdpid=prodserver schema=ds ;
data mydata ;
set tdata.enterprise_table ;
run ;

Resources