Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A similar question was answered in <a href="https://stackoverflow.com/questions/337704/parameterizing-an-sql-in-clause">Parameterize an SQL IN clause</a></p> <p>The same idea can be applied here:</p> <pre><code>declare @EmailIds varchar = '|email1@test.com|email2@test.com|'; UPDATE dbo.My_Users SET MyUserId=@MyUserId WHERE @EmailIds LIKE '%|' + EmailID + '|%'; </code></pre> <p>Though this does not contain a comma-separated list, the delimiter could easily be changed to a pipe-character. The caveat here is, the more data that exists in the table and the more email addresses that are in the @EmailIds list, the slower (much slower) this query can become.</p> <p>Using C#, I would actually recommend the second example in the above-mentioned question where the list is expanded to create a query similar to:</p> <pre><code>UPDATE dbo.My_Users SET MyUserId=@MyUserId WHERE EmailID IN (@email1, @email2); </code></pre> <p>C# to implement (a modified version of the example in the question above):</p> <pre><code>string[] emails = new string { "email1@test.com", "email2@test.com" }; string sql = "UPDATE dbo.My_Users SET MyUserId=@MyUserId WHERE EmailID IN ({0});" string[] emailParams = emails.Select((s, i) =&gt; "@email" + i.ToString()).ToArray(); string inClause = string.Join(",", emailParams); using (SqlCommand cmd = new SqlCommand(string.Format(sql, inClause))) { for(int i = 0; i &lt; emailParams.Length; i++) { cmd.Parameters.AddWithValue(emailParams[i], emails[i]); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload