0

I have to create an external service for existing database which is working with ERP system.

Edit: service will be running on the same machine where SQL Server is running.

Service has to listen for a trigger on a table with documents.

Scenario:

  1. User creates a document
  2. Trigger calls service method
  3. Method queries the database to get data, create document and send it to external API.

Is it possible to catch trigger like that from a C# Worker Service?

1 Answer 1

3

It's technically possible to run arbitrary SQL CLR code in a trigger, and make remote web service or rpc calls to code running in a separate service. But since the trigger runs during the transaction and any code you run can delay or undo the database change, it's not recommended. And probably would not be supported by the ERP system.

So the best pattern here is to have the trigger write a row into a local table, and then have an external process poll that table and do the rest of the work, or to configure Change Data Capture or Change Tracking and have the external program query that.

4
  • This sounds more like a comment to me then an answer
    – GuidoG
    Commented Mar 14, 2022 at 15:45
  • 1
    Thanks for reply! So maybe i should just query documents table directly?
    – Sadzio98
    Commented Mar 14, 2022 at 15:52
  • Service will be running the same machine where SQL Server is running. Just calling external API
    – Sadzio98
    Commented Mar 14, 2022 at 15:59
  • Any external access, including RPC, file IO, or network IO in a trigger is strongly discouraged. Also adding triggers to tables you don't control is usually a bad idea. Commented Mar 14, 2022 at 16:25

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.