Skip to content

update query not working using IN operator #6211

Answered by pacmano1
manojkvn asked this question in Q&A
Discussion options

You must be logged in to vote

A WHERE IN needs parentheses around values.

...WHERE ColumnName IN ('Value1', 'Value2', 'Value3');  -- or a list of other data types of course.

You should also be using parameterized calls to the database.

And this way of calling SQL (credit to @tonygermano for showing me this a long time ago) can be a bit easier to read.

var params = new java.util.ArrayList();
var list_of_ids = [1, 2, 3];  // List of IDs
var placeholders = list_of_ids.map(function() { return '?'; }).join(', ');
var sql = 'UPDATE junk SET test = \'a\' WHERE id IN (' + placeholders + ')';
list_of_ids.forEach(function(id) {
      params.add(id);
  });
dbConn.executeUpdate(sql, params);

There can be times where you may need …

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@manojkvn
Comment options

@tonygermano
Comment options

Answer selected by manojkvn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants