1
DELETE FROM tblArtworkApprovalUsers
WHERE     (userID NOT IN (@UserIDList)) AND (approvalID =
                          (SELECT     ID
                            FROM          tblArtworkApprovals
                            WHERE      (templateID = @TemplateID)))

This is in my table adapter. @UserIDList needs to accept something like:

2,44,12,70

How can I make this query accept that string?

Tom Gullen
  • 61,249
  • 84
  • 283
  • 456

3 Answers3

2

NOT IN <expr> requires an expression and not a string. So if you are passing the parameter and not constructing the SQL dynamically this cannot be done.

Alternative is to create the SQL dynamically (while being aware of SQL Injection):

string commad = @"DELETE FROM tblArtworkApprovalUsers " +
             "WHERE (userID NOT IN ({0})) AND (approvalID ="+
                          "(SELECT     ID " +
                            "FROM          tblArtworkApprovals " +
                            "WHERE      (templateID = {1})))";

command = string.Format(command, userIDList, templateID);

UPDATE

Craig pointed to a better solution here which does not provide much better performance (since parameters are variable and query plan does not get cached unless it is exactly the same) but help with SQL injection attack: Parameterize an SQL IN clause

Community
  • 1
  • 1
Aliostad
  • 80,612
  • 21
  • 160
  • 208
  • Thanks! How do I pass in a command to a table adapter though? – Tom Gullen Mar 10 '11 at 12:12
  • Here's how to parameterize an IN clause, I assume same holds true for NOT IN: http://stackoverflow.com/questions/337704/parameterizing-a-sql-in-clause/337792#337792 – Craig Mar 10 '11 at 12:12
1

You have a couple of options...

  1. Dynamically create the whole SQL string and execute (like Aliostad suggests).
  2. Write a stored procedure that accepts the string, parses it into a temporary table, then run your query against that temp table. A quick search will provide many ways to do this (here is one).
Ocelot20
  • 10,510
  • 11
  • 55
  • 96
1

You might want to have a look at Arrays in SQL2005 and Arrays in SQL2008, depending on the version of your SQL server.

Gorgsenegger
  • 7,356
  • 4
  • 51
  • 89