Sqlbulkcopy is a new object provided by. net2.0.
Because this object is mainly used to improve the efficiency of batch insertion, insertion of batch data by default does not trigger the insert trigger. However, this trigger must be used in some cases. You can use sqlbulkcopy (string, sqlbulkcopyoptions) to specify Enum sqlbulkcopyoptions as firetriggers.
There is a bookkeeping function in the project, and the balance of each record should be calculated from the previous increase and decrease of the balance and the current account. The original statement is to insert new records into each item of the front-end reconciliation, and use the insert trigger to calculate the balance. Insert triggers are written as follows:
Select a. parentid, max (A. Stamp) stamp into # temp1
From ivinventoryitem A, inserted B
Where a. parentid = B. parentid and A. oid B. OID
Group by A. parentid
Select @ lastcurrentqty1 = isnull (T. currentqty1, 0), @ lastcurrentqty2 = isnull (T. currentqty2, 0 ),
@ Lastcurrentqty3 = isnull (T. currentqty3, 0), @ lastcurrentqty4 = isnull (T. currentqty4, 0)
From ivinventoryitem t
Inner join # temp1 IV on T. parentid = IV. parentid and T. Stamp = IV. Stamp
Drop table # temp1
-- Number of existing instances = number of existing instances + number of incomes-Number of expenditures
Update ivinventoryitem set currentqty1 = @ lastcurrentqty1 + isnull (inserted. depositqty1, 0)-isnull (inserted. withdrawalqty1, 0 ),
Currentqty2 = @ lastcurrentqty2 + isnull (inserted. depositqty2, 0)-isnull (inserted. withdrawalqty2, 0 ),
Currentqty3 = @ lastcurrentqty3 + isnull (inserted. depositqty3, 0)-isnull (inserted. withdrawalqty3, 0 ),
Currentqty4 = @ lastcurrentqty4 + isnull (inserted. depositqty4, 0)-isnull (inserted. withdrawalqty4, 0)
From inserted where ivinventoryitem. OID = inserted. OID
*/
-- Number of existing instances = number of existing instances + number of incomes-Number of expenditures
Update ivinventoryitem set currentqty1 = @ lastcurrentqty1 + isnull (inserted. depositqty1, 0)-isnull (inserted. withdrawalqty1, 0 ),
Currentqty2 = @ lastcurrentqty2 + isnull (inserted. depositqty2, 0)-isnull (inserted. withdrawalqty2, 0 ),
Currentqty3 = @ lastcurrentqty3 + isnull (inserted. depositqty3, 0)-isnull (inserted. withdrawalqty3, 0 ),
Currentqty4 = @ lastcurrentqty4 + isnull (inserted. depositqty4, 0)-isnull (inserted. withdrawalqty4, 0)
From inserted where ivinventoryitem. OID = inserted. OID
Because the system is a large number of business processing systems, when the number of accounts per time is large (> 2000), this method has obvious performance problems. Therefore, the front-end is changed to sqlbulkcopy, which significantly improves the performance. However, the insert trigger does not work. The original sqlbulkcopy object is mainly used to improve the efficiency of batch insertion. Therefore, by default, batch data insertion does not trigger the insert trigger. You can use sqlbulkcopy (string, sqlbulkcopyoptions) to specify Enum sqlbulkcopyoptions as firetriggers. However, the calculation of the overall balance after the trigger operation is incorrect. The above trigger is analyzed and found that it uses the records in the inserted temporary table to obtain the balance of the last record inserted this time. However, because sqlbulkcopy is used to insert multiple records at a time, one record cannot be found from inserted in each trigger. In fact, in an insert or update transaction, the new row is added to both the inserted Table and the trigger table. Therefore,UseSqlbulkcopyIn insertedThe table contains multiple records and calls sqlbulkcopy at a time.Only one trigger runs.
Change the trigger as follows, and the test count is normal:
Select a. parentid, max (A. Stamp) stamp into # temp1
From ivinventoryitem A, inserted B
Where a. parentid = B. parentid and A. oid B. OID
Group by A. parentid
-- Number of existing instances = number of existing instances + number of incomes-Number of expenditures
Update ivinventoryitem set
Currentqty1 = isnull (P. currentqty1, 0) + isnull (I. depositqty1, 0)-isnull (I. withdrawalqty1, 0 ),
Currentqty2 = isnull (P. currentqty2, 0) + isnull (I. depositqty2, 0)-isnull (I. withdrawalqty2, 0 ),
Currentqty3 = isnull (P. currentqty3, 0) + isnull (I. depositqty3, 0)-isnull (I. withdrawalqty3, 0 ),
Currentqty4 = isnull (P. currentqty4, 0) + isnull (I. depositqty4, 0)-isnull (I. withdrawalqty4, 0)
From ivinventoryitem C, (select * From ivinventoryitem) P, # temp1 T, inserted I
Where p. parentid = T. parentid and P. Stamp = T. Stamp and T. parentid = I. parentid
And C. oid = I. oid and (isnull (I. currentqty1, 0) + isnull (I. currentqty2, 0) + isnull (I. currentqty3, 0) + isnull (I. currentqty4, 0) = 0)
Drop table # temp1