I'm struggling to transfer SQL query results into R after successfully retrieving data in RStudio's SQL Results tab.
What I've Done So Far
- Connected to the database via RStudio's "Connections" tab.
- Executed SQL queries using the SQL editor, successfully previewing results:
-- !preview conn=con
SELECT xdailyissues.issue_date, xdailyissues.drugfull, xdailyissues.quantity_container,
xdailyissues.quantity_doseunits, xdailyissues.issue_type, xdailyissues.patient_hospitalno,
xdailyissues.costcentre, xdailyissues.lnkdid
FROM %PARALLEL JAC_Super.xdailyissues xdailyissues
WHERE (xdailyissues.issue_date >= {d '2025-04-01'} AND xdailyissues.issue_date <= {d '2025-04-30'})
- The SQL Results tab shows my query output correctly.
- However, I can't find a way to transfer these results into an R variable.
Attempts That Didn't Work
I've tried various approaches, including
query <- "SELECT * FROM %PARALLEL JAC_Super.xdailyissues xdailyissues WHERE (xdailyissues.issue_date >= '2025-04-01' AND xdailyissues.issue_date <= '2025-04-30')"
issues <- dbGetQuery(con, query)
… but I keep getting ODBC errors.
Question How can I transfer the SQL query results that are already captured in the SQL Results tab into an R dataframe, without re-executing the query manually?
I want to avoid unnecessary external data refreshes, especially given Power BI’s unpredictable behaviour (see Power BI Loading Large Tables Unnecessarily – How to Prevent Unwanted Refreshes?). Any guidance would be greatly appreciated!
Error messages received:
Error in `dbGetQuery()`:
! ODBC failed with error HY000 from [Iris ODBC][State : HY000][Native Code 400].
✖
• [C:\Program Files\RStudio\resources\app\bin\rsession-utf8.exe]
• [SQLCODE: <-400>:<Fatal error occurred>]
• [Error: <<UNDEFINED>Compile+17^%SYS.SQLSRV *%qinfo("type")>]
• [Location: <Prepare>]
• <SQL> '-- !preview conn=con SELECT "xdailyissues"."issue_date", "xdailyissues"."drugfull",
"xdailyissues"."quantity_container", "xdailyissues"."quantity_doseunits", "xdailyissues"."issue_type",
"xdailyissues"."patient_hospitalno", "xdailyissues"."costcentre", "xdailyissues"."lnkdid" FROM %PARALLEL
"JAC_Super"."xdailyissues" "xdailyissues" WHERE ("xdailyissues"."issue_date">={d '2025-04-01'} AND
"xdailyissues"."issue_date"<={d '2025-04-30'}) '
ℹ From nanodbc/nanodbc.cpp:1726.
Run `rlang::last_trace()` to see where the error occurred.
> query <- "SELECT xdailyissues.issue_date, xdailyissues.drugfull, xdailyissues.quantity_container, xdailyissues.quantity_doseunits, xdailyissues.issue_type, xdailyissues.patient_hospitalno, xdailyissues.costcentre, xdailyissues.lnkdid
+ FROM %PARALLEL JAC_Super.xdailyissues xdailyissues
+ WHERE (xdailyissues.issue_date >= {d '2025-04-01'} AND xdailyissues.issue_date <= {d '2025-04-30'})"
> issues <- dbGetQuery(con, query)
Error: nanodbc/nanodbc.cpp:2856: 2201
[Iris ODBC][State : 22018 ][Native Code 22005]
[C:\Program Files\RStudio\resources\app\bin\rsession-utf8.exe]
Error in assignment
Warning message:
In dbClearResult(rs) : Result already cleared
> query <- "SELECT * FROM %PARALLEL JAC_Super.xdailyissues xdailyissues WHERE (CAST(xdailyissues.issue_date AS DATE) >= '2025-04-01' AND CAST(xdailyissues.issue_date AS DATE) <= '2025-04-30')"
> issues <- dbGetQuery(con, query)
Error in `dbGetQuery()`:
! ODBC failed with error from .
✖
• <SQL> 'SELECT * FROM %PARALLEL JAC_Super.xdailyissues xdailyissues WHERE (CAST(xdailyissues.issue_date AS DATE) >=
'2025-04-01' AND CAST(xdailyissues.issue_date AS DATE) <= '2025-04-30')'
ℹ From nanodbc/nanodbc.cpp:1726.
Run `rlang::last_trace()` to see where the error occurred.
Please note that I managed to pull a table with the following code in RStudio before. I wasn't able to repeat this, however, and received the error messages above with my later attempts.
query <- "SELECT \"xdailyissues\".\"issue_date\", \"xdailyissues\".\"drugfull\", \"xdailyissues\".\"quantity_container\", \"xdailyissues\".\"quantity_doseunits\", \"xdailyissues\".\"issue_type\", \"xdailyissues\".\"patient_hospitalno\", \"xdailyissues\".\"costcentre\", \"xdailyissues\".\"lnkdid\"
+ FROM %PARALLEL \"JAC_Super\".\"xdailyissues\" \"xdailyissues\"
+ WHERE (\"xdailyissues\".\"issue_date\" >= {d '2024-04-01'} AND \"xdailyissues\".\"issue_date\" <= {d '2025-03-31'})
+ AND (\"xdailyissues\".\"costcentre\" LIKE 'Q4A%' OR \"xdailyissues\".\"costcentre\" LIKE 'Q4C%' OR \"xdailyissues\".\"costcentre\" LIKE 'Q4D%')
+ AND (\"xdailyissues\".\"drugfull\" LIKE 'Zopiclone%' OR \"xdailyissues\".\"drugfull\" LIKE 'Zolpidem%')
+ "
dbGetQueryfetches everything.odbc(R package), some fromDBI(R package). Please include the literal text of warnings and errors, I don't want to try to guess. (2) It seems unlikely this is related to the RStudio IDE. Unless you can demonstrate that it works in Rgui and not in RStudio, the rstudio tag is inappropriate. Similarly for powerbi: if you can demonstrate things work in R/RStudio and they don't work in PBI, then the tag is appropriate.conis not formed correctly in R, but not knowing the rest of your PBI doc, it's a little hard to know for sure.dbGetQuery, yet not consistently. I also managed to use the same SQL code successfully with PowerBI. Both PowerBI and RStudio use the exact same ODBC user DSN.