6

Good Afternoon to All,

I have a question concerning on SQL Queries. is it possible to use an array as a parameter to a query using the "IN" command?

for example,

int x = {2,3,4,5}

UPDATE 'table_name' set 'field' = data WHERE field_ID IN (x)

the reason I am asking this is to avoid an iterative SQL Statement when I have to update data in a database. I also thought of using a for each statement in for the UPDATE Query but I don't know if it will affect the performance of the query if it will slow down the system if ever 100+ records are updated.

I am using VB.Net btw. My Database is MySQL Workbench.

7
  • generate from array string (2, 3, 4, 5) UPDATE ... WHERE ID IN {cond} REPLACE string {cond} on string get from array Commented Aug 27, 2013 at 7:52
  • so i need to convert first my Integer Array into a String is that what you are implying,? Commented Aug 27, 2013 at 8:01
  • Yes. Mysql not supported array-parameters and table-parameters. Commented Aug 27, 2013 at 8:03
  • @realnumber3012, using plain sql could result in sql injection, so i think Law was asking for a proper way to do some workaround for the case of IN statement Commented Aug 27, 2013 at 8:07
  • @Rex correct sir. btw 2,3,4,5 was just an example, the array could be of any size depending on the number of selections Commented Aug 27, 2013 at 8:13

3 Answers 3

3

I have gotten the answer. I just simply need to convert each elements to a String then concatenate it with a "," for each element. so the parameter that i will pass will be a string.

ANSWER: int x = {2,3,4,5} will become string x = "2,3,4,5"

My Query string will become "UPDATE tablename SET field=value WHERE ID IN("&x&")"

Thank you to all who helped.

Sign up to request clarification or add additional context in comments.

Comments

2

If you have the query in a variable (not a stored procedure) and you don't have a huge amount of ids, you could built your own IN. I haven't tested the speed of this approach.

This code won't compile, it's just to give you an idea.

query = "SELECT * FROM table WHERE col IN ("

For t = 0 TO x.Length-1
    If t > 0 Then query &= ","

    query &= "@var" & t
Next

query &= ")"

...

For t = 0 TO x.Length-1
    cmd.Parameters.Add("@var" & t, SqlDbType.Int).Value = x(t)
Next

Comments

1

i am not familiar with mySQL, but when dealing with MSSQL, I normally have a split function in DB so that I can use it to split concatenated integer values as a table, at VB side, something like:

        Dim myIds = {1, 2, 3, 4}
        Dim sql = <sql>                          
                    SELECT m.* FROM dbo.tblMyData m
                    INNER JOIN dbo.fncSplitIntegerValues(@Ids, ',') t ON t.id = m.Id
                  </sql>.Value

        Using con As New SqlConnection("My connection string..."),
            cmd As New SqlCommand(sql, con)

            cmd.Parameters.Add("@Ids", SqlDbType.VarChar).Value =
                myIds.Select(Function(m) m.ToString).Aggregate(Function(m, n) m & "," & n)

            con.Open()

            Dim rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
            While rdr.Read()
                Console.WriteLine(rdr.GetValue(0))
                ' do something else...
            End While

        End Using

dbo.fncSplitIntegerValues is a db function to split concatenated integer varchar into integer Id with given separator.

it's important not to use plain sql, instead, use sql parameters.

Note: above sample is not tested...

1 Comment

my idea would be to convert the elements to a string for it to be passed to the query. thanks for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.