1

I have a sql query like

SELECT SOME_FIELD FROM SOME_TABLE WHERE ID_FIELD IN ("abc", "def", "...", ...)

And an array that i need to pass as a parameter

p := []interface{"abc", "def", "blah", "foo"}

I prepare the statement:

stmt, err := conn.Prepare("SELECT SOME_FIELD FROM SOME_TABLE WHERE ID_FIELD IN (:1)")
rows, err := stmt.Query(p) // <- this complains that I pass an array as a value

I know that i can convert that array into a comma separated string of quoted values, but i wonder if there is a better way of passing an array of values as a parameter to a query

For moderators who closed my question previously: There is a similar question How to pass variable ids to statement.Query() in golang? but it is not the same. The answer to that question suggests unpacking the array when passing to the Query, like stmt.Query(...p) - but this will not work in my case as this will be treated as a number of parameters instead of one array parameter

Andrey
  • 20,487
  • 26
  • 108
  • 176

0 Answers0