Showing posts with label bit. Show all posts
Showing posts with label bit. Show all posts

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.

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.

sql

Sunday, March 11, 2012

Calling a stored procedure inside another stored procedure (or "nested stored procedures")

Hi all - I'm trying to optimized my stored procedures to be a bit easier to maintain, and am sure this is possible, not am very unclear on the syntax to doing this correctly. For example, I have a simple stored procedure that takes a string as a parameter, and returns its resolved index that corresponds to a record in my database. ie

exec dbo.DeriveStatusID 'Created'

returns an int value as 1

(performed by "SELECT statusID FROM statusList WHERE statusName= 'Created')

but I also have a second stored procedure that needs to make reference to this procedure first, in order to resolve an id - ie:

exec dbo.AddProduct_Insert 'widget1'

which currently performs:
SET @.statusID = (SELECT statusID FROM statusList WHERE statusName='Created')
INSERT INTO Products (productname, statusID) VALUES (''widget1', @.statusID)

I want to simply the insert to perform (in one sproc):

SET @.statusID = EXEC deriveStatusID ('Created')
INSERT INTO Products (productname, statusID) VALUES (''widget1', @.statusID)

This works fine if I call this stored procedure in code first, then pass it to the second stored procedure, but NOT if it is reference in the second stored procedure directly (I end up with an empty value for @.statusID in this example).

My actual "Insert" stored procedures are far more complicated, but I am working towards lightening the business logic in my application ( it shouldn't have to pre-vet the data prior to executing a valid insert).

Hopefully this makes some sense - it doesn't seem right to me that this is impossible, and am fairly sure I'm just missing some simple syntax - can anyone assist?

Two steps:

1. Create the SP to return new ID like:

CREATE PROCEDURE [dbo].[deriveStatusID] @.Created Varchar(100),

@.statusIDint OUTPUT

AS

SELECT@.statusID =statusID FROM statusList WHEREstatusName=@.Created

2. Modify your second SP as:

....

Declare @.NewStatusId Int

EXECderiveStatusID 'Created', @.NewStatusId OUTPUT

INSERT INTO Products (productname, statusID) VALUES (''widget1',@.NewStatusId)