0

I'm new to SQL Server and I have situation that I want to instantly receive inserted data to my web service url.

So I want to create a trigger in SQL Server to send all inserted data to my web service url.

Can you please help me with this.

6
  • 1
    I would strongly recommend not doing any (potentially) long running tasks like calling upon external resources in a trigger. The trigger is executed in the context and transaction of the statement that caused it to fire, and that transaction will only complete once the trigger is done. Therefore, a trigger should be very nimble and fast - not do any "heavy lifting" or extensive processing! Commented Apr 3, 2018 at 4:35
  • 1
    can we do this ON AFTER INSERT event ?? Commented Apr 3, 2018 at 4:51
  • No - "ON INSERT" and "AFTER INSERT" are the same thing, really Commented Apr 3, 2018 at 4:53
  • What exactly do you mean by web service url? Commented Apr 3, 2018 at 5:07
  • @MK_ web service url means any url in my server or you can say API url that will receive data and will process received data. Commented Apr 3, 2018 at 5:26

1 Answer 1

1

Calling a service from trigger is really a bad idea. Even you should avoid calling any service from SQL Server, it should be done from outside the SQL server. You can implement your requirement by following way.

On every insert, using trigger store the newly inserted row inside a table (queue) for processing by some background job. This will make sure that your insert performance will not impact and chances of failure will be minimum.

You can choose one of the below option.

  1. Processing Service : You can create a windows service, which will pool the queue table, it will pick the pending items and will call the required service. After successfully completion it should updated the queue status to processed.

  2. SQL Job : Write a SQL job and schedule it to run after a fixed interval of time like (1 minute). This job will pick the pending items from your queue table and will use the CLR stored procedure sp_OACreate to call the service.

This will not be real time, but more reliable option with less chances of failure. You can make to near real time by increasing the frequency of job/service.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.