0

I am trying to use dplyr to pull data from a table on a linked SQL Server. I am able to connect and run queries using the dbGetQuery method as long as I (1) provide the fully qualified path ([server name].[database name].[schema name].[table name]) and (2) set up the dbConnect method without a DB parameter (so that the connection is to the server at large, not a specific DB). If I try to connect to the DB on the linked server I get a 'not found' error.

So I'm working with a server-level connection, but when I connect to the server and try to pull data from the SQL tables into a tbl object I get an 'invalid object name' error.

I am running

``tbl(con, dbplyr::in_schema("[server name].[database name].[schema name]", "table name")``

I have tried the same with in_catalog without success.

Is there a way to use these methods in dplyr to pull data from a linked server? I'd rather not resort running a select * if I can avoid it.

I am expecting to create a table in R studio based on the data in the linked server table. I am instead getting an error saying that I provided an 'invalid object name'. I have tried in_schema and in_catalog. I have confirmed the connection details as well.

4
  • 1
    I think it could help if you could give more context. For instance, what code are you using when you are "able to connect and run queries as long as I provide the fully qualified path ([server name].[database name].[schema name].[table name])"? Commented Dec 19, 2023 at 23:53
  • 2
    That code probably causes it to create a schema name like [[server name]].[database name]].[schema name]]]. Commented Dec 20, 2023 at 1:47
  • I agree David, it's nesting them in a way that isn't really what I'm hoping for. Not sure how to undo that. Commented Dec 20, 2023 at 14:19
  • Do you mean dbplyr with this? I'd suggest you change the tag for clarity? Commented Dec 20, 2023 at 15:43

2 Answers 2

1

This problem is caused by how in_schema operates. Similar to my answer here, we need to tell dbplyr that the server-database-schema string is already prepared sql.


Explanation

The command dbplyr::in_schema("text1", "text2") wants to treat all of text1 as the schema name, and all of text2 as the table name.

This causes a problem in a case like:

in_schema("[server name].[database name].[schema name]", "table name")

As [server name].[database name].[schema name] is not one large schema name but the combination of three separate names together. However, during translation, dbplyr wraps the entire argument in delimiters so that it is treated as if it were a single schema name.


Solution

Thankfully there is a way to prevent dbplyr from treating the entire argument as a single schema name: The sql() function tells dbplyr to treat its contents as if they were already converted to SQL.

So the solution is:

in_schema(sql("[server name].[database name].[schema name]"), "table name")

Some additional caution is required when using this method. Because dbplyr will not apply delimiters, it is best to ensure your inputs have their own delimiters.

----------------------------------

Update

There are now two other approaches available:

  1. in_catalog is designed to receive database and schema name. For example: in_catalog("database","schema","table").

  2. The dbplyr documentation for in_schema and in_catalog now recommends I() instead. For example: I("database.schema.table"). With I() you should be able to use square-brackets if needed: I("[database].[schema].[table]").

Sign up to request clarification or add additional context in comments.

Comments

0

You could also use DBI:Id(). For example, Id(schema = "schema name", table = "table name").

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.