Thursday, March 22, 2012

Calling native code from a trigger.

I've been searching for a bit but I can't seem to find a difinitive answer.

Can an SQL Server trigger call native C/C++ functions? If so, what is the mechanism?

Thanks in advance.

My knee-jerk reaction is that this sounds like a very bad idea, but perhaps not. What are you trying to accomplish?|||Moving thread to the more appropriate forum.|||

You could do that by creating a CLR procedure or function that wrapped the Win32 API calls.

What are you attemping to accomplish?

|||I was thinking of having a trigger that would call a native function that could do some needed tasks outside the SQL server. Things like post a message to an open mail slot on either the same or a different server so that another service can perform some work. After talking with on of the other developers, we will probably send the messages from his app. that is connected to the SQL server. I was thinking of having a trigger do it because it would remove him from having to manage the messages. Also a thought for future development.|||

Personally, I wouldn't have a TRIGGER engage any outside service. It provides too many additional surfaces for disruption -the TRIGGER hangs waiting for the outside service ...

A better approach is to have the TRIGGER insert the relevant data into a queueing table.

Then have a separate SQL Agent Job that constantly runs checking the queue, and upon finding a row of data, processes that row in whatever fashion is appropriate (sending mail, logging, etc.)

|||

In my case, it would just post a message into a queue and a thread would be watching the queue. The processing would happen on another process or service running multi threaded code. The trigger would probably return in a few ms. This would probably be faster than a lot of ODBC calls and the other service can simply wait on an event using no CPU cycles until an event is signalled.

Different ways to skin the cat I guess. Coders like to have code do the work and DBA's like the DBMS to do the work. Also a factor of what you are comfortable with.

No comments:

Post a Comment