0

I'm using R to analyze a table that I first query with SQL

The problem is the table (schema) name is a reserved word.

The following query results in error:

# Establish connection
conn <- DBI::dbConnect(odbc::odbc(), "myDb")

# Write SQL query
query <- 'SELECT * FROM "GENERIC".table1 WHERE column1 = "RandomText"'

# Pull data
DBI::dbGetQuery(conn, query)

ERROR: column "RandomText" does not exist;

RandomText isn't a column to begin with. It seems as if I can't use the quotes twice, but it is required to pass the query as a string to DBI function.

How can I query SQL in R when the table has a reserved name in it?

The link below did not help me solve the issue in R

Similar question addressing the same issue in Postgres directly

I'm using PostgreSQL

EStark
  • 161
  • 4
  • 18

1 Answers1

0

I found a solution to my question - using an escape character (\) and switching quote characters worked:

# Write SQL query
query <- "SELECT * FROM \"GENERIC\".table1 WHERE column2 = 'RandomText'"
EStark
  • 161
  • 4
  • 18