views:

567

answers:

2

I have a situation where I have a bunch of SQL Update commands that all need to be executed. I know that DataSets can do batch updates, but the only way I've been able to accomplish it is to load the whole table into a dataset first. What if I want to only update a subset of the records in a table?

+1  A: 

EDITED: Based on your comment, I'd recommend using the Bulk Copy method to go to a staging table first. Then you can do a single update on your real table based on the staging table.

=========

One way is to build the SQL Command yourself; however, I'd recommend reading about the SQL Injection possibilities to protect yourself. Depending on your situation and your platform there are other options.

For example if you are dealing with a lot of data, then you could do a bulk import into a holding table, which you would then issue a single update command off of. I've also had good success passing records in as XML (I found in my environment that I needed at least 50 rows to offset the cost of loading the DOM, and I knew that the scalability issues were not a factor in my case).

Some other things I've seen was people package the updates into a binary field, for example one binary field for each column, then they have a function which unpacks the binary field. Again you will want to test with your environment.

With that said, have you verified that simply calling a single update command or stored procedure for each update you need is not sufficent? You might not even need to batch the commands up.

  • Josh

Watch out for Premature Optimization it can be killer!

JoshBerke
I'm dealing with 1000's/sec. I can handle adds with SQLBulkCopy, and deletes can be batched into a single command ("... where uniqueId in (...)"). But updates are a problem. The only way I have found to do them is one at a time. ... and yes it performs poorly.
dviljoen
Ok then why not use SQLBulkCopy to add to a temporary holding table then run a single update on your main table from the temp table
JoshBerke
That's a great suggestion. Sorry for the delay in replying. I'm going to try that. Any suggestions on the sql for autoupdating the records from the temp table to the real one? A stored proc?
dviljoen
No problem:-) Stored Proc will work, if you dyanamicaly create real tables you might need dynamic sql...pick something that works first get it working then test then pptimize;-)
JoshBerke
+2  A: 

The easiest way out here is load table with the required rows (rows you wan to update). Double check that the RowState is "Inserted". Assign the InsertCommand property of the adapter with your stored procedure (wrapped in an SqlCommand) that does the "update", this tweak will ensure that all the rows present in the table gets updated.

The fundamental here being: The DataAdapter runs the UpdateCommand on rows for which state is Updated, runs InsertCommand for rows which state is Inserted and finally DeleteCommand for the rows for which state is Deleted.

Binoj Antony