Skip to content
Advertisement

How to use IN clause with SQLParameters?

My function works perfectly if I provide one instance of ComputerName value. My question is how to modify SQL query so it could accept an array? I don’t believe creating a loop to query a database several times is a proper way. For example, I think this SQL query with IN clause is a proper way:

In other words, I’d like to be able to call this function providing multiple ComputerName values. Like this:

The code I need help with. Please use SQLParameters to build SQL query:

Advertisement

Answer

You need to do a little bit of string manipulation, also when working with SQL queries is a lot easier to use Here Strings.

If you are going to pass multiple computers to your functions, the parameter $PCname has to be able to accept an array of strings, hence changing [string] to [string[]].

Check out this code and see if it works for you:

Here is how the query should look like:

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement