Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
Log in  ::  Register  ::  Not logged in

SQL Server Alerts In C++ Builder

By Stas Vasiljev,

Sometimes we need receive alert on client side about some event on SQL server without request. For instance, data is changed and we want notify all client applications.It is possible to realize this option with extended stored procedure contained in dynamic-link library, that send broadcast packets through sockets via UDP. The creation of an extended stored procedure was made in С++ Builder 6, with use of ODS (Open Data Service) API for MS SQL Server 2000. To take into consideration that static library Opends60.lib with  this Borland IDE, which realize all services of  ODS API, has outdated version and support only MS SQL 7. You may get import library file there or create it self-dependent with  Implib utility. Also, to be noticed that UDP not ensure the message delivery, but don’t require a connection, as TCP. This is main reason why UDP was choosen.

    The simple example of using alerts is generation event from user audit table’s trigger after adding new record. The structure of “EVENT” table consist of unique record identifier, user login and message. The extended stored procedure "xp_event"  may has following input parameters:

  • <host name>
  • <port number>
  • <message text>
  • <user name>
  • <record identifier>
You may use broadcast address in host name argument. For instance, (net-directed broadcast ) or (limited broadcast address). Also you may use the local network computer’s name. Notice, that if your net has subnets, then router didn’t admit any broadcast packet without addition settings. The number of UDP port is optional, but you should avoid system ports that are using by OS. By default, the client listen 3338 port.

    The TSQLAlerter component has two methods: Start and Stop, which creates  new process for port listening and stop it correspondingly, i.e. the client has UDP server roles. The event OnGetMessage comes in the moment of notification delivery, and the pointer to TLabel visual component allows display a getting message at the form. The structure are using for sending broadcast messages has the following view:

typedef struct TDATASEND // The structure for sending alerts
char message[1024];
char login[1024];
long id;

    The thread gets an alert and synchronize properties Message, RecordId and Login of TSQLAlerter  class object in AddMessage() method. The example of registration procedure and realization of delivery notification you may look in script.


-- Only add an extended stored procedure to the master database.

USE master

-- If 'xp_event' already exist, drops an extended stored procedure.

IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'xp_event' AND type = 'X')
EXEC sp_dropextendedproc 'xp_event'

/* sp_addextendedproc adds entries to the sysobjects table, registering the name of the new extended
stored procedure with SQL Server. It also adds an entry in the syscomments table.
First argument is the name of the function to call within the dynamic-link library procedure,
second argument is the name of the DLL containing the function.*/

EXEC sp_addextendedproc xp_event, 'dll_event.dll'

-- If table 'EVENTS' already exist, drops table.

IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'EVENTS' AND type = 'U')

/*Create 'EVENTS' table*/


-- If trigger 'events_trg' already exist, drops trigger.

IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'events_trg' AND type = 'TR')
DROP TRIGGER events_trg

/*Create 'events_trg' trigger*/

declare @login varchar(20)
declare @message varchar(2000)
declare @id int
select @login=rtrim(LOGIN),@id = ID from inserted
select @message = EVENTS.MESSAGE from inserted join EVENTS on EVENTS.ID=inserted.ID
-- To execute an extended stored procedure from a database
-- other than master, qualify the name of the extended stored procedure with master.
-- 'xp_event' parameters: <host name or broadcast address>, <port number>,
-- <message text>, <user name>, <record identifier>.

EXEC master..xp_event '',3338,@message,@login,@id

We used TSQLAlerter component in own bug-tracking system, for notifications programmers and quality assurance engineers when bug's status was close or changed.


Total article views: 4750 | Views in the last 30 days: 1
Related Articles

Extended Procedure

Extended Procedure


Extended Events Objects

So far, I have talked about some of the metadata associated with Extended Events. But I have not yet...


Extended Events Categories

I recently delved into a brief explanation of Packages as they correlate to Extended Events. In that...


Extended stored procedure

Extended stored procedure


extended stored procedure

extended stored procedure


Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones