Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preview SQL Query Results for Parametrized queries #4824

Closed
dfajardo84 opened this issue May 22, 2019 · 11 comments
Closed

Preview SQL Query Results for Parametrized queries #4824

dfajardo84 opened this issue May 22, 2019 · 11 comments
Labels
enhancement stale Issues which have been closed automatically due to inactivitiy.

Comments

@dfajardo84
Copy link

When a SQL script is created which includes placeholders (?s) for parametrization using dbBind, RStudio returns an error when trying to preview the query as the query isn't valid until values are passed to the placeholders.

Ideally, there would be a way to define default values for each of the parameters in the first line along with the connection.

@ronblum
Copy link
Contributor

ronblum commented May 24, 2019

@dfajardo84 Thanks for reporting this. Can you please provide an example so that we can try to reproduce this? Also, the following information would be useful:

System details

RStudio Edition : <!-- Desktop or Server -->
RStudio Version : 
OS Version      : 
R Version       : 

@ronblum ronblum added the info needed Additional information requested—reprex, steps, open question, etc. label May 24, 2019
@dfajardo84
Copy link
Author

RStudio Edition : Desktop
RStudio Version : Version 1.2.1335
OS Version : Windows 10 Pro
R Version : 3.5.3 (2019-03-11)

I can't provide a fully working example because I don't have a table to reference, but say I have a connection "con" to a database with a table named "table" with a column with name "col_name".

The following SQL file will return an error - "Failed to query database: Query needs to be bound before fetching"

-- !preview conn=con
SELECT *
FROM table
WHERE col_name = ?

@ronblum
Copy link
Contributor

ronblum commented May 29, 2019

@dfajardo84 Ah! Thank you. I'll mark this as a (useful) enhancement.

@ronblum ronblum added enhancement and removed info needed Additional information requested—reprex, steps, open question, etc. labels May 29, 2019
@dfajardo84
Copy link
Author

Awesome.

If I were to recommend a way to address this, it would be to past a list of parameters to the magic first line, something along the lines of:

-- !preview conn=con, param=(1, "Los Angeles", 254.99)

@ronblum
Copy link
Contributor

ronblum commented May 31, 2019

@dfajardo84 I'm reopening the issue—the open state tells us that the issue should be reviewed, both for enhancements and bugs. (Closed tells us the issue is resolved.)

@ronblum ronblum reopened this May 31, 2019
@dfajardo84
Copy link
Author

Oh, I must have accidentally hit close and comment instead of just comment. My bad.

@ronblum
Copy link
Contributor

ronblum commented May 31, 2019

No problem! I've done the same plenty of times. :)

@gcameron89777
Copy link

Just adding my voice since it looks like this has been open for a little while. This would be a awesome feature! https://community.rstudio.com/t/passing-variables-between-r-and-sql-scripts-while-still-being-able-to-use-script-preview/52358/4

The ability to frictionlessly shift contexts between r and sql is a really great analytics experience. This works already when using SQL chunks in Rmd docs and I feel more productive with this set up.

@stale
Copy link

stale bot commented Feb 5, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs, per https://github.com/rstudio/rstudio/wiki/Issue-Grooming. Thank you for your contributions.

@stale stale bot added the stale Issues which have been closed automatically due to inactivitiy. label Feb 5, 2021
@stale
Copy link

stale bot commented Feb 19, 2021

This issue has been automatically closed due to inactivity.

@stale stale bot closed this as completed Feb 19, 2021
@jsowder
Copy link

jsowder commented Feb 17, 2023

This functionality hasn't been added, but in the meantime I suggest setting a SQL variable using DBI::DbExecute(con, "set var1 = value1") then referencing that SQL variable in your script, eg: "SELECT $var1". I've confirmed this works with SQL Preview in Rstudio. You'll just need to run the DbExecute prior to using the SQL Preview.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement stale Issues which have been closed automatically due to inactivitiy.
Projects
None yet
Development

No branches or pull requests

4 participants