Can't Get Enough RSS 2.0
 Sunday, December 16, 2007

I developed a small application for my office, in this application I used a trigger to read data from a web service, then update the table after the record is inserted.

Everything was working fine, except... the web service sometimes takes time to come with the result, what keeps the user waits for a long time before he gets a confirmation that the record was saved.

I thought about an asynchronous trigger, I did some google, and found that asynchronous triggers can be done with Sql Server's Service Broker, little bit from here and there, I built the following as a template for my asynchronous triggers:

 

USE master

GO

-- Check if database exists and drop it

IF EXISTS(SELECT * FROM sys.databases where name = 'AsyncTrigger')

DROP DATABASE AsyncTrigger

GO

-- Create the database

CREATE DATABASE AsyncTrigger

GO

-- Enable Service Broker

ALTER DATABASE AsyncTrigger

SET ENABLE_BROKER

GO

USE AsyncTrigger

GO

-- Create our test table

CREATE TABLE Table_1(

[id] [int] PRIMARY KEY IDENTITY(1,1) NOT NULL,

[Col1] [int] NOT NULL,

[Col2] [int] NOT NULL,

[Sum] [int] NULL)

GO

-- This procedure will run asynchronously and execute our task

CREATE PROCEDURE usp_Processor

AS

BEGIN

SET NOCOUNT ON;

DECLARE @Handle UNIQUEIDENTIFIER;

DECLARE @MessageType SYSNAME;

DECLARE @Message XML;

RECEIVE TOP (1)

@Handle = conversation_handle,

@MessageType = message_type_name,

@Message = message_body

FROM [MyQ];

IF(@Handle IS NOT NULL AND @Message IS NOT NULL)

BEGIN

DECLARE @tmpT Table(id int, Col1 int, Col2 int, [Sum] int);

insert into @tmpT SELECT

Table1.Column1.value('@ID', 'INT') as id,

Table1.Column1.value('@Col1', 'INT')as col1,

Table1.Column1.value('@Col2', 'INT') as col2,

Table1.Column1.value('@Sum', 'INT') as [sum]

FROM @Message.nodes('/row') AS Table1(Column1)

Update Table_1 set [Sum] = tmpT.Col1 + tmpT.Col2

from @tmpT as tmpT Inner Join Table_1 as T

on tmpT.id = T.id;

END -- End If

END -- End Procedure usp_Processor

GO

-- Create Message Type

CREATE MESSAGE TYPE [MyMessage] VALIDATION = WELL_FORMED_XML;

GO

-- Create Contract

CREATE CONTRACT [MyContract] ([MyMessage] SENT BY ANY);

GO

-- Create Queue

CREATE QUEUE [MyQ] WITH ACTIVATION

(STATUS = ON, MAX_QUEUE_READERS = 1,

PROCEDURE_NAME = usp_Processor,

EXECUTE AS OWNER);

GO

-- Create Service

CREATE SERVICE [MyService] ON QUEUE [MyQ] ([MyContract]);

GO

-- This trigger will send our message to the queue

CREATE TRIGGER Table_1_Insert ON Table_1 AFTER INSERT

AS

BEGIN

SET NOCOUNT ON;

DECLARE @MessageBody XML;

set @MessageBody = (SELECT ID, Col1, Col2, [Sum] FROM Inserted FOR XML RAW, TYPE);

DECLARE @Handle UNIQUEIDENTIFIER;

BEGIN DIALOG CONVERSATION @Handle

FROM SERVICE [MyService]

TO SERVICE 'MyService'

ON CONTRACT [MyContract]

WITH ENCRYPTION = OFF;

SEND ON CONVERSATION @Handle

MESSAGE TYPE [MyMessage](@MessageBody);

END

GO

 

Now execute the following:

 

Insert Into Table_1 (Col1, Col2) Values (34, 78)

Select * From Table_1

Waitfor Delay '00:00:03'

Select * From Table_1

 

The result will be:

id             Col1        Col2        Sum
----------- ----------- ----------- -----------
1              34           78           NULL

 

id             Col1        Col2        Sum
----------- ----------- ----------- -----------
1              34           78           112

Sunday, December 16, 2007 5:01:14 PM UTC  #    Comments [2] -
SQL Server
Wednesday, July 01, 2009 3:28:37 AM UTC
hm.. asynchronous triggers is not actually triggers, but queueing triggers..?.. helpful example. thanks.
Sunday, August 30, 2009 8:01:52 AM UTC
Best quality example, Thank You!
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview
Navigation
Categories
Archive
<March 2010>
SunMonTueWedThuFriSat
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Ghazi Sarhan
Sign In
Statistics
Total Posts: 13
This Year: 0
This Month: 0
This Week: 0
Comments: 4
Themes
Pick a theme:
All Content © 2010, Ghazi Sarhan
DasBlog theme 'Business' created by Christoph De Baene (delarou)