简体   繁体   中英

SQL Server invoke action on insert rows that will add data to another table async from C#

I have C# process that insert rows to table in SQL Server 2012.
I want to insert tracking information to another 2 tables as a result with a specific logic.

I thought about 3 ways to do it:

  1. Create stack/collection to will collect the data and will insert by Entity Framework each X seconds/mili.

  2. SQL Server trigger that will listen to events on the table and will insert the data to another table. In case of trigger the action will be synchronized and will slow the operation.

  3. Async trigger action - Trigger on the table that will send to the service broker queue and will execute it async by procedure.

What is the best way or maybe there is another way?

Unless you are confident that the overhead on insert in case 2. is unacceptable, go with it. This way your solution is as good as having two more inserts each time/in the same transaction an insert to the main table is performed.

Thus you will be sure you are not missing inserts due to some unavoidable failure and no need to worry about restarting copy process.

I would personally start with this solution first, profile and only then consider other options. Actually, I am going to use it in a project of my own.

If you can read a bit about OUTPUT Clause in sql, maybe it can fit best in what you're trying to achieve. Maybe browse this for examples

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM