15,964,752 members
See more:
Hi, could you please help me on how I should make a select statement where the condition is equal to the elements of an array.

Thank you.
Posted
Updated 2-Jan-14 11:44am
v2
CHill60 2-Jan-14 9:35am
Not without some more information! Post what you have tried so far
Karthik_Mahalingam 2-Jan-14 10:19am
BK 4 code 2-Jan-14 23:37pm
Please let us confirm that you want condition based on
1st Assumption column_name as column and array element as value resides in that column.
And 2nd assumption is the table whose columns name as a value in that array.

## Solution 1

If you mean "where the condition is one of 1, 2, or 3" then try this:
SQL
`SELECT * FROM MyTable WHERE MyColumn IN (1,2,3)`

Aarti Meswania 2-Jan-14 12:49pm
short n simple
5!+ :)
Member 10215709 2-Jan-14 18:15pm
Aarti Meswania 2-Jan-14 23:41pm
It was for OriginalGriff
please use "Have a Question or Comment? " :)
OriginalGriff 3-Jan-14 1:56am
You're welcome!

## Solution 2

To add to what Griff said, if you have the array in code, you need to either build a SQL string in code ( bad ), or you can use XML or a table based variable to pass the values in. If you pass in a string of '1,2,3', unless you use SQL to split on the ,, it's not going to work for you. I hope to have an article on doing this up within the next two days.

## Solution 3

For integer array you can use this..
C#
```int[] intarrayItems = { 1, 2, 3, 4, 5 };
string intitems ="";
intarrayItems.ToList().ForEach(k=> intitems += k + ",");
intitems = intitems.Trim(',');
string sqlint = "SELECT * FROM MyTable WHERE MyColumn IN ( " + intitems+ " )";
// "SELECT * FROM MyTable WHERE MyColumn IN ( 1,2,3,4,5 )"

//for string array ..
// string values
string[] stringarrayItems = { "one","two","three","four" };
string stritems = "";
stringarrayItems.ToList().ForEach(k => stritems += "'" + k + "',");
stritems = stritems.Trim(',');
string sqlstr = "SELECT * FROM MyTable WHERE MyColumn IN ( " + stritems + " )";
//"SELECT * FROM MyTable WHERE MyColumn IN ( 'one','two','three','four' )"```

Christian Graus 2-Jan-14 23:37pm
Of course, you would NEVER do this, because it opens you to SQL injection attacks ( int list is probably find, but it's still best on principle to never build SQL this way ).

Given that your code presumes LINQ, a LINQ to SQL solution would have been better.
Karthik_Mahalingam 3-Jan-14 0:26am
yes agreed.
Member 10215709 3-Jan-14 14:32pm
Thanks to all of you for your help
Karthik_Mahalingam 3-Jan-14 23:56pm
welcome dude :)