代码之家  ›  专栏  ›  技术社区  ›  Nova

R中的dbReadTable错误:对象名无效

  •  1
  • Nova  · 技术社区  · 6 年前

    我在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'. 
    
    2 回复  |  直到 6 年前
        1
  •  3
  •   Sturgus    6 年前

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

        2
  •  0
  •   franiis Lukas Owen    6 年前

    同样的问题。我确实发现它与表的模式有关。您可以通过首先设置频道来列出架构:

    ch = odbcDriverConnect(DSM, Uid, Pwd) 
    

    然后运行:

    tables = sqlTables(ch)
    

        3
  •  0
  •   Richard van der Pool    5 年前

    dbplyr包允许您选择要与in\u schema函数一起使用的模式。这应该能奏效。

    library(dbplyr)
    con_tlkpSampleStatus <- tbl(con, in_schema("abc", "tlkpSampleStatus"))