我在 R 中建立了一个连接:
conn <- dbConnect(odbc::odbc(),
Driver = "SQL Server",
Server = "...",
Database = "AdventureWorks2012")
默认的 ODBC 驱动程序是 AdventureWorks2012。我可以很好地看到数据集,例如,以下代码运行没有问题:
dbGetQuery(conn, "SELECT TOP 10 * FROM Person.Person")
另一方面,跑步
dbReadTable(conn, "Person.Person")
产量:
Error: <SQL> 'SELECT * FROM "Person.Person"'
nanodbc/nanodbc.cpp:1587: 42S02: [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name 'Person.Person'.
同样,运行
data <- tbl(conn, "Person.Person")
产量:
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'.
另外,我很困惑WHERE (0 = 1)
:无法弄清楚它来自哪里!
(1) 该表确实存在于数据库中。(2) 我用正确的 Schema 限定了名称。(3) 我已将 ODBC 中的默认数据库设置为 AdventureWorks2012。
我将感谢您的帮助和洞察力。谢谢你 ...
> 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'.