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

How to handle @Param in a SQL IN clause #2

Open
joranE opened this issue Jul 27, 2016 · 5 comments
Open

How to handle @Param in a SQL IN clause #2

joranE opened this issue Jul 27, 2016 · 5 comments

Comments

@joranE
Copy link

joranE commented Jul 27, 2016

Many times we have SQL like:

select 
  col1,
  col2
from
  my_table
where
  col3 in @param

I tried:

> sql %>% sq_set(param = '(1,2,3)')
select 
  col1,
  col2
from
  my_table
where
  col3 in '(1,2,3)'

and,

sql %>% sq_set(param = 1:3)
select 
  col1,
  col2
from
  my_table
where
  col3 in 1
Warning message:
In gsub(paste0(prefix, pattern), value, query, perl = TRUE) :
  argument 'replacement' has length > 1 and only the first element will be used

and,

> sql %>% sq_set(param = "1,2,3")
select 
  col1,
  col2
from
  my_table
where
  col3 in '1,2,3'

None result in valid SQL.

There should be a way to do this, probably.

@smbache
Copy link
Owner

smbache commented Jul 27, 2016

R lists will be converted to SQL lists, so:

sql %>% sq_set(param = list(1,2,3))

should work, or if you have a vector, vec:

sql %>% sq_set(param = as.list(vec))

@joranE
Copy link
Author

joranE commented Jul 27, 2016

Ah, ok. Thanks, IN clauses are always the first thing I try with these sorts of SQL tools.

@smbache
Copy link
Owner

smbache commented Jul 27, 2016

Glad I have that one covered, then :) I guess I should document this in the README!

@ThoDuyNguyen
Copy link

This is exactly my problem.

Thank you for pointing out.

<3

@BriBecker
Copy link

The IN clause is always something I worry about first. Great!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants