Using ODBC::dbConnect and dplyr to connect to Sybase IQ database - table name not found

半世苍凉 提交于 2020-07-08 03:41:27

问题


I can connect to my Sybase IQ 16 database using a connection string such as:

myDB_conn <- dbConnect(odbc(), "MyDSN_Name")

When I run this command the connections view shows a list of databases and corresponding tables/views. However when I try to use the logic laid out here specifically naming the view I keep receiving an error about my view not being found.

test <- tbl(myDB_conn, "OFFSHORE_BOB.SOME_VIEW_OR_TABLE_NAME")

In the connection window I can see the database and view but when I use the code above I receive a table name not found.

nanodbc/nanodbc.cpp:1374: 42S02: [Sybase][ODBC Driver][Sybase IQ]Table 'OFFSHORE_BOB.SOME_VIEW_OR_TABLE_NAME' not found

I have also looked at listing the tables in the OFFSHORE_BOB schema and I can see the table of interest in there:

y <- dbListTables(gopher_conn, schema = 'OFFSHORE_BOB')

I then searched for the view of interest and I can see it there so I am stumped as to why I can not use this code

test <- tbl(myDB_conn, "OFFSHORE_BOB.SOME_VIEW_OR_TABLE_NAME")

Any suggestions would be greatly appreciated.


回答1:


You most likely want the in_schema command from dbplyr. For example:

schema = "OFFSHORE_BOB"
tbl_name = "SOME_VIEW_OR_TABLE_NAME"

remote_table <- tbl(db_connection, from = in_schema(schema, tbl_name))

Then show_query(remote_table) should return something like:

SELECT *
FROM "OFFSHORE_BOB"."SOME_VIEW_OR_TABLE_NAME"

(The quotation marks are inserted by dbplyr in case your table or schema name contains non-standard characters. E.g. abc-123 and 3-4 are not acceptable table names by themselves, but quoted "abc-123" and "3-4" they can be table or schema names.)

Note that you can also include a database name in with the schema name should you need to:

remote_table <- tbl(db_connection, from = in_schema(paste0(db,".",schema), table_name))


来源:https://stackoverflow.com/questions/62723999/using-odbcdbconnect-and-dplyr-to-connect-to-sybase-iq-database-table-name-no

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!