Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid object name Error when querying SQL Server Database from R using ODBC

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'. 
like image 401
Sasha Babaei Avatar asked Oct 16 '22 20:10

Sasha Babaei


1 Answers

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"))

?

like image 51
epi_n00b Avatar answered Nov 01 '22 19:11

epi_n00b