8

Possible Duplicate:
Parameterizing an SQL IN clause?

Suppose I had a table of schools which school_name, school_enrolment

As input to my program, someone types a list of schools that they would like to see the enrolment for. Instead of generating an sql query like:

SELECT * FROM school_table
WHERE
school_name = 'program_input_1' or school_name = 'program_input_2' or school_name = 'program_input_3'

is it possible or straightforward to do something like

SELECT * from school_table
WHERE
school name in [array of program inputs]

as a much cleaner way of writing this?

Community
  • 1
  • 1
user1771624
  • 143
  • 1
  • 1
  • 4

2 Answers2

10

Yes, this is what IN is for:

SELECT col1, col2, ..., coln
FROM school_table
WHERE school_name IN ('program_input_1', 'program_input_2', 'program_input_3')
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
2

You can use the IN(...) clause:

WHERE School_name in ('school 1','school 2')

In terms of passing an array, it depends on the programming language you are using to generate the SQL.

You would have to write a custom function that loops over the array, dynamically generating the IN clause

Leigh
  • 28,765
  • 10
  • 55
  • 103
Lock
  • 5,422
  • 14
  • 66
  • 113