我一直在搜寻stackoverflow和Google一个小时,试图弄清为什么我的表无法显示。到目前为止,对其他人有用的解决方案对我不起作用。
我正在连接到这样的数据库:
library(DBI)
library(dplyr)
con <- dbConnect(odbc::odbc(), .connection_string = "Driver={SQL Server};",
user = rstudioapi::askForPassword("Your computer usename e.g. SmithJo"),
password = rstudioapi::askForPassword("Your computer password (e.g. to login to Windows)"),
port = 1433,
server = "myserver",
dbname = "MYDB")
连接到某些表有效:
traptable <- dbReadTable(con, "tblTrap")
其他人则不,即使表存在!
> dbExistsTable(con, "tlkpSampleType")
[1] TRUE
我已经提供了一些我尝试过的解决方案...
> dbReadTable(con, "tlkpSampleType")
Error: <SQL> 'SELECT * FROM "tlkpSampleType"'
nanodbc/nanodbc.cpp:1587: 42S02: [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name 'tlkpSampleType'.
> dbReadTable(con, SQL("tlkpSampleType"))
Error: <SQL> 'SELECT * FROM tlkpSampleType'
nanodbc/nanodbc.cpp:1587: 42S02: [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name 'tlkpSampleType'.
> dbGetQuery(con, "SELECT * FROM tlkpSampleStatus")
Error: <SQL> 'SELECT * FROM tlkpSampleStatus'
nanodbc/nanodbc.cpp:1587: 42S02: [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name 'tlkpSampleStatus'.
> dbReadTable(con,"MYDB.tlkpSampleStatus")
Error: <SQL> 'SELECT * FROM "MYDB.tlkpSampleStatus"'
nanodbc/nanodbc.cpp:1587: 42S02: [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name 'MYDB.tlkpSampleStatus'.
> queryResults <- dbGetQuery(con, "SELECT * FROM [MYDB].[abc].[tlkpSampleStatus]")
Error: <SQL> 'SELECT * FROM [MYDB].[abc].[tlkpSampleStatus]'
nanodbc/nanodbc.cpp:1587: 42S02: [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name 'MYDB.abc.tlkpSampleStatus'.
> dbReadTable(con, Id(schema = "tlkpSampleStatus"))
Error: <SQL> 'SELECT * FROM "tlkpSampleStatus"'
nanodbc/nanodbc.cpp:1587: 42S02: [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name 'tlkpSampleStatus'.
> dbReadTable(con, Id(schema = "MYDB", table = "tlkpSampleStatus"))
Error: <SQL> 'SELECT * FROM "MYDB"."tlkpSampleStatus"'
nanodbc/nanodbc.cpp:1587: 42S02: [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name 'MYDB.tlkpSampleStatus'.
> dbGetQuery(con, paste('SELECT * FROM', 'tlkpSampleStatus'))
Error: <SQL> 'SELECT * FROM tlkpSampleStatus'
nanodbc/nanodbc.cpp:1587: 42S02: [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name 'tlkpSampleStatus'.
> dbReadTable(con, Id(schema = "tlkpSampleStatus", table = "tlkpSampleStatus"))
Error: <SQL> 'SELECT * FROM "tlkpSampleStatus"."tlkpSampleStatus"'
nanodbc/nanodbc.cpp:1587: 42S02: [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name 'tlkpSampleStatus.tlkpSampleStatus'.
答案 0 :(得分:3)
将查找表保存在与存储表不同的模式中并不少见。无论哪种方式,都应在可能时用表名称来限定表名。例如,
>dbReadTable(con, SQL("dbo.tlkpSampleType"))
要查看表及其模式的完整列表,可以执行以下操作:
1 rs <- dbSendQuery(con, "SELECT SCHEMA_NAME(schema_id) As SchemaName, name As TableName FROM sys.tables;")
2 dbFetch(rs)
从那里,您可以适当地限定表以供将来查询。
答案 1 :(得分:0)
这里有同样的问题。我确实发现这与表的架构有关。您可以通过首先设置频道来列出架构:
ch = odbcDriverConnect(DSM, Uid, Pwd)
然后运行:
tables = sqlTables(ch)
我发现我可以读入具有dbo模式而不是sys的表。
答案 2 :(得分:0)
通过dbplyr软件包,您可以选择与in_schema函数一起使用的模式。那应该可以解决问题。
library(dbplyr)
con_tlkpSampleStatus <- tbl(con, in_schema("abc", "tlkpSampleStatus"))