Goal: be able to conduct SQL queries on a data frame in R.
Approach used: using dbWriteTable to write the table to the database that I would then be able to query on using SQL and join to other tables existing in the DB.
Issue: Seems to execute successfully, but table does not seem to actually exist in the db. Errors thrown when attempting to query table. Details below:
Data frame name: testing_df = 1 column dataframe
channel <- DBI::dbConnect(odbc::odbc(), "data_source_name", uid="user_name", pwd='password')
dbGetQuery(channel,"use role role_name;")
dbGetQuery(channel,"use warehouse warehouse_name;")
dbGetQuery(channel,"use schema schema_name;")
dbGetQuery(channel,"use database db_name;")
table_name = Id(database="database_name",schema="schema_name",table="table_testing")
dbWriteTable(conn = channel,
name = table_name,
value = testing_df,
overwrite=TRUE)
dbReadTable(channel,name=table_name)
dbExistsTable(channel,name=table_name)
dbReadTable provides output of data frame expected.
dbExistsTable provides the following output:
> dbExistsTable(channel,name=table_name)
[1] TRUE
Issue: The table cannot be located in the actual database UI, and when running the following in R:
desired_output <- dbGetQuery(channel,sprintf("select * from database_name.schema_name.table_testing;"))
desired_output
I get the following error:
SQL compilation error: Object 'table_testing' does not exist or not authorized.
I am able to check in the database and see that the table actually does not exist.
Question: Does anyone know if dbWriteTable is actually supposed to be writing the table to the database, or if I'm misunderstanding the purpose of dbWriteTable? Any better ways to approach this task?