I am trying to use the SQL query to count the number of rows of myid in Table_1 data frame. The data frame is already present in R Environment and I retrieved it by following query.
Table_1 <- dbGetQuery(conn, "Select * FROM Patients") .
Now to count() the number of rows of myid in Table_1, I use following query and it says
count_myid <- dbGetQuery(conn, "Select count(myid) FROM Table_1")
Invalid object name 'Table_1'[Microsoft][odbc sql server driver]
[sql server] Statement(s) could not be prepared
Whereas, I checked the above mentioned query for another Table and it works perfectly fine for another table. I spent hours on this to figure out the problem but I couldn't that what is actually wrong in this query and why its not working. I know it can be done using different functions of R. I did used dbReadTable() and all the same queries mentioned in this Question, but not working.
If I try to retrieve it directly from SQL database and not from the R Environment, then it works fine. From SQL DB, I use the following query.
count_myid <- dbGetQuery(conn, "Select count(myid) FROM MyProj.dbo.Patients")
dbGetQueryonly uses theconnconnection, so queries cannot see anything in your R environment.sqldfpackage:sqldf::sqldf("select count(myid) FROM Table_1"). (Note thatsqldfis not going to know aboutPatients, since that table is not local.)librarycommands to know your actual DB API used and please tag your RDBMS type. AFAIKDBI::dbGetQueryin major APIs (RPostgreSQL, odbc, RSQLite, RMariaDB, ROracle) cannot read from R environment but only objects fromconnDB connection. Please carefully compare all tables and views in all schemas:SELECT TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE FROM INFORMATION_SCHEMA.TABLES ORDER BY TABLE_SCHEMA, TABLE_NAME. Do not go off memory but actually check.