Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One option is to create a temp table on the server and then use the bulk load facility to upload all the IDs into that table at once. Then use a join, EXISTS or IN clause to delete only the records that you uploaded into your temp table.</p> <p>Bulk loads are a well-optimized path in SQL Server and it should be very fast.</p> <p>For example:</p> <ol> <li>Execute the statement <code>CREATE TABLE #RowsToDelete(ID INT PRIMARY KEY)</code></li> <li>Use a bulk load to insert keys into <code>#RowsToDelete</code></li> <li>Execute <code>DELETE FROM myTable where Id IN (SELECT ID FROM #RowsToDelete)</code></li> <li>Execute <code>DROP TABLE #RowsToDelte</code> (the table will also be automatically dropped if you close the session)</li> </ol> <p>(Assuming Dapper) code example:</p> <pre><code>conn.Open(); var columnName = "ID"; conn.Execute(string.Format("CREATE TABLE #{0}s({0} INT PRIMARY KEY)", columnName)); using (var bulkCopy = new SqlBulkCopy(conn)) { bulkCopy.BatchSize = ids.Count; bulkCopy.DestinationTableName = string.Format("#{0}s", columnName); var table = new DataTable(); table.Columns.Add(columnName, typeof (int)); bulkCopy.ColumnMappings.Add(columnName, columnName); foreach (var id in ids) { table.Rows.Add(id); } bulkCopy.WriteToServer(table); } //or do other things with your table instead of deleting here conn.Execute(string.Format(@"DELETE FROM myTable where Id IN (SELECT {0} FROM #{0}s", columnName)); conn.Execute(string.Format("DROP TABLE #{0}s", columnName)); </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.
    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