一個查詢插入多行我的userIds
與使用C#陣列
List<int> userIds = new List<int>();
我有代碼中插入它們到分貝的列表。
ExecuteSQL(sql)
是我自己的函數,抽象了所有的數據庫特定的邏輯
string iSql = "";
foreach(int id in userIds){
iSql = "insert into users (id) values (" + id + ")";
ExecuteSQL(iSql);
}
我想插入所有這些在一個查詢,而不必調用像上面的循環, 這可能在SQL?
如果是這樣,那麼string iSql
會是什麼樣子?
我發現Inserting multiple rows in a single SQL query?,
但可能創造一個大的字符串。
,我發現其他文章要麼其他語言或只是困惑,因爲我無法它們與我的問題
(後來我纔可以照顧參數的,我只需要查詢)
當你得到了一些答案,我認爲1000使用可以插入了值(1),(2)。如果超過1000則可以使用TVP。 http://msdn.microsoft.com/en-us/library/bb510489.aspx – Paparazzi
參考http://lennilobel.wordpress.com/2009/07/29/sql-server-2008-table-valued-parameters-and -C-定製迭代器-A-比賽做在天堂/ – Paparazzi