0

can i pass a List into a SqlCommand as a paramter?

DELETE FROM MyTable WHERE Key IN @MyParam

command.Parameters.AddValue("MyParam",MyList);

i realize i could just loop over the list but i was looking for a cleaner solution.

gbn
  • 6,079
  • 1
  • 18
  • 21
KellCOMnet
  • 245
  • 2
  • 11

2 Answers2

2

No, you can't in the current form

There are several options using UDFs or XML or dynamic SQL to pass in lists.

The most comprehensive article on how to do this is "Arrays and Lists in SQL Server 2005 and Beyond" by Erland Sommarskog

gbn
  • 6,079
  • 1
  • 18
  • 21
0

Pass them as a table type parameter,

CREATE TYPE dbo.CategoryTableType AS TABLE  
    ( CategoryID int )  

then consume it in your procedure

CREATE PROCEDURE usp_UpdateCategories
    (@tvpNewCategories dbo.CategoryTableType READONLY)  

and do stuff like

delete from dbo.Categories where id in (select CategoryID from @tvpNewCategories) ;