Soner Gönül
Soner Gönül

Reputation: 98740

Adding Parameter SQL IN

I just say if i have a SQL like this;

SELECT B.HESAP_NO
FROM S_TEKLIF B
WHERE B.HESAP NO = 234

I can add some parameters this SQL programaticly like this C# code;

      if (txtBoxText1 != "")
      {
          strQuery = strQuery + " AND A.ISL_TAR >= @S_TARIH_B";

          dt_stb = DateTime.Parse(txtBoxText1);
          myCommand.Parameters.AddWithValue("@S_TARIH_B", dt_stb);
      }

With this code, i can add AND "A.ISL_TAR >= @S_TARIH_B" to i my SQL.

BUT, in SQL IN operators i don't know how can i add some parameters.

For example;

SELECT A.HESAP_NO
FROM S_TEKLIF A
WHERE A.HESAP_NO IN (SELECT A.HESAP FROM S_TEKLIF WHERE A.MUS_K_ISIM = 
for (int counter = 0; counter < list.Items.Count; counter++)
  {
 if (list.Items[counter].Selected)
{
 values.Add(list.Items[counter].Value);
 }

  })

How can i add a parameters to SQL IN ?

Upvotes: 0

Views: 499

Answers (2)

SWeko
SWeko

Reputation: 30882

There are a number of ways to achieve this, and all of them are described in the Arrays and Lists in SQL Server article. It's a gread read for any SQL developer.

Upvotes: 2

MatBailie
MatBailie

Reputation: 86706

I'm not aware of any way to pass a list/array as a single parameter for use as part of an IN clause. Your best option may be to build up the IN clause yourself as a string.

Alternatively, you could do this in steps.
1. Create a temp table with just an ID field
2. Create a parameterised query to insert one value into that temp table
3. Loop through your values, calling the sql to insert values into the temp table
4. Exexute your main SQL query, joining on to the temp table instead of using IN

You just need to ensure you don't close and re-open you session each time, thus ensuring your temp table is persisted and retains the values you've been inserting.

Upvotes: 0

Related Questions