6

I have setup a connection in R:

conn <- dbConnect(odbc::odbc(),
                 Driver = "SQL Server",
                 Server = "...",
                 Database = "AdventureWorks2012")

The default ODBC driver is AdventureWorks2012. I can see the data set just fine and for example, the following code runs without a problem:

dbGetQuery(conn, "SELECT TOP 10 * FROM Person.Person")

On the other hand, running

dbReadTable(conn, "Person.Person")

yields:

Error: <SQL> 'SELECT * FROM "Person.Person"'
  nanodbc/nanodbc.cpp:1587: 42S02: [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name 'Person.Person'. 

Similary, running

data <- tbl(conn, "Person.Person")

yields:

Error: <SQL> 'SELECT *
FROM "Person.Person" AS "zzz12"
WHERE (0 = 1)'
  nanodbc/nanodbc.cpp:1587: 42S02: [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name 'Person.Person'.

Also, I am stumped as to WHERE (0 = 1) : cannot figure out where it comes from!!!

(1) The table does exist in the database. (2) I have qualified the name with the correct Schema. (3) I have set the default database in ODBC to AdventureWorks2012.

I would appreciate your help and insight. Thank You ...

> tableList <- dbListTables(conn)
> tableList
  [1] "AWBuildVersion"                                      
  [2] "DatabaseLog"                                         
  [3] "DeleteExample"                                       
  [4] "EmployeeSales"                                       
  [5] "ErrorLog"                                            
  [6] "PersonCross"                                         
  [7] "PhoneCross"                                          
  [8] "Department"                                          
  [9] "Employee"                                            
 [10] "EmployeeDepartmentHistory"                           
 [11] "EmployeePayHistory"                                  
 [12] "JobCandidate"                                        
 [13] "Shift"                                               
 [14] "Address"                                             
 [15] "AddressType"                                         
 [16] "BusinessEntity"                                      
 [17] "BusinessEntityAddress"                               
 [18] "BusinessEntityContact"                               
 [19] "ContactType"                                         
 [20] "CountryRegion"                                       
 [21] "EmailAddress"                                        
 [22] "Password"                                            
 [23] "Person"                                              
 [24] "PersonPhone"
 ...

> dbReadTable(conn, "Person")
Error: <SQL> 'SELECT * FROM "Person"'
  nanodbc/nanodbc.cpp:1587: 42S02: [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name 'Person'. 
Sasha Babaei
  • 455
  • 3
  • 8
  • I don't know much about odbc stuff, but that 'SELECT * FROM "Person.Person"' stuff seems strange. I don't think "-quotes should be used for an object's name, maybe try dbReadTable(conn, Person.Person)? – George Menoutis Apr 04 '18 at 19:53
  • According to [AdventureWorks](https://technet.microsoft.com/en-us/library/ms124438(v=sql.100).aspx), there is no `Person.Person` table. There is `Person.Contact` and `Sales.SalesPerson`. – Parfait Apr 04 '18 at 21:10
  • AdventureWorks2012 has a `Person.Person` table. – Sasha Babaei Apr 11 '18 at 07:27
  • Did you ever figure this out Sasha? – Nova Sep 06 '18 at 16:37

3 Answers3

3

I came across this same problem and discovered an answer in the dbplyr documentation:

https://db.rstudio.com/best-practices/schema/ where it includes this error message and says you must do: tbl(con, in_schema("production", "flights"))

However, "production" here is specific to your database.

It took me a while to figure out what my schema name was, but it turns out it was just the name of the database. So, maybe you could try

    conn <- dbConnect(odbc::odbc(),
             Driver = "SQL Server",
             Server = "...",
             Database = "AdventureWorks2012")
    data <- tbl(conn, in_schema("AdventureWorks2012", "Person"))

or

    data <- tbl(conn, in_schema("Person", "Person"))

?

epi_n00b
  • 150
  • 3
  • 15
1

Since AdventureWorks maintains no default schema (i.e., no dbo), consider using odbc::dbId to properly select the table under such a schema as shown in this Git issue comment:

conn <- dbConnect(odbc::odbc(),
                  Driver = "SQL Server",
                  Server = "...",
                  Database = "AdventureWorks2012")

tbl <- dbId(conn, "Contact", "Person")
dbReadTable(conn, tbl)

NOTE: Support for schemas is relatively new for odbc package per one of the authors as recently as 15 days ago. You may have to re-install a Git development version if CRAN version is too old.

Parfait
  • 104,375
  • 17
  • 94
  • 125
  • Thank you. The dbId method is not still in the stable master branch. I would rather not utilise that. – Sasha Babaei Apr 06 '18 at 00:22
  • Understood. Curious, what happens when you don't put any period qualifying schema but just query the table name? Also, what does `dbListTables(conn)` show? – Parfait Apr 06 '18 at 03:07
  • Please edit your post to show output of `dbListTable(conn)`. Can you not use exact name in a `SELECT` query? – Parfait Apr 11 '18 at 16:41
0

I found I was getting this error when I was using a connection string with an 'Initial Catalogue=dbname' setting, rather than 'Database=dbname':

# This works
connectionString <- "Driver={ODBC Driver 17 for SQL Server};Server=tcp:dbServer,1433;Database=dbName;Persist Security Info=False;UID=dbUser;PWD=dbPassword;MultipleActiveResultSets=False;Connection Timeout=30;"

conn<-dbConnect(odbc::odbc(), .connection_string = connectionString)
dbReadTable(conn, "Person")
zola25
  • 1,774
  • 6
  • 24
  • 44