Update is being executed before BulkInsert

bulkinsert sqlbulkcopy sql-server vb.net

Question

I wrote a program to insert some records using sqlCopy for faster processing, and then the code executes and update statement [on an other table] based on the newly inserted records.

Problem is that the update almost always executes before the insert! [about 2 seconds before.. according to the time-stamps of the insert and update rows.] the only way I could make the update execute after the insert is to put the code asleep for 2-3 seconds.. Is there a better way to make sure the insert completes before the code continues?

I even captured the RowsCopied event, the rows are instantly copied 'before' the update. but in the database the update gets in earlier.

     bulkCopy.WriteToServer(table)

     Dim Sql = "Update tbl Set Total = (select sum(qty) from Inserttbl where inId = ID)"
     ExecuteSQL(Sql)

Accepted Answer

Found the problem... the insert timestamp was provided by the client, while the update timestamp came from the server. The clients time is 2 seconds earlier then the servers time..

I changed both should use the servers getDate function. problem solved.



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why