Click here to Skip to main content
15,888,454 members
Articles / Database Development / SQL Server
Article

SQL Server extended stored procedure to send UDP messages

Rate me:
Please Sign up or sign in to vote.
3.20/5 (11 votes)
25 Apr 20051 min read 81.3K   1.1K   27   17
SQL Server extended stored procedure to send UDP messages.

Introduction

I wanted to be able to announce SQL Server table updates so I modified the Microsoft sample extended stored procedure xp_hello. The source for this article will produce a DLL that can be placed in the \Program Files\Microsoft SQL Server\MSSQL\Binn directory and called from a regular stored procedure to send a UDP message. I chose UDP so there would be minimal execution time added (no TCP connection issues).

Be aware that this extended stored procedure is executed in SQL Server’s space so any errors/exceptions could crash SQL Server. This happened to me during development when I mistakenly called WSACleanup after sending an announcement, causing SQL Server to lose its network connections. In short, be very careful what you do within the DLL as it can affect SQL Server.

A sample SQL Server stored procedure that calls SQLAnnounceProc is:

SQL
CREATE PROCEDURE getTestTable
AS
BEGIN SELECT * FROM TEST_TABLE
declare @status int
declare @ret varchar(513)
declare @temp as varchar(128)
SET @temp = 'got ' + CAST( @@ROWCOUNT as varchar(16) ) + ' rows'
declare @ip varchar(32)
SET @ip = '127.0.0.1'
declare @port varchar(16)
SET @port = '15555'
exec @status = master.dbo.SQLAnnounceProc @ret OUTPUT, @ip, @port, @temp
END
GO

Of course, you will need to create a table named TEST_TABLE for testing purposes and call it from the Query Analyser using:

SQL
use TEST {call getTestTable}

Notes:

  • You can view the UDP messages by running the Perl script announceListen.pl.
  • Add directory \Program Files\Microsoft SQL Server\80\Tools\DevTools\Include to VC++ include dirs.
  • Add directory \Program Files\Microsoft SQL Server\80\Tools\DevTools\Lib to VC++ lib dirs.

This is a new version with the following changes:

  1. Bug fix. There was a 255 char limit on the UDP message text because I used srv_paramdata(), which has a 255 char limit. I changed the code in SQLAnnounceProc.c to use srv_paraminfo() instead. The rest of the params still have the 255 char limit.
  2. I converted the project to VS 2003.
  3. Test before use!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I enjoy working with C#, Java, Perl and C++

Comments and Discussions

 
General64-bit version Pin
Mark Nadelson17-May-07 10:57
Mark Nadelson17-May-07 10:57 
QuestionBug with some buffer ? Pin
Pablo7531-Aug-06 5:21
Pablo7531-Aug-06 5:21 
AnswerRe: Bug with some buffer ? Pin
Pablo7531-Aug-06 8:06
Pablo7531-Aug-06 8:06 
GeneralRe: Bug with some buffer ? Pin
Rod VanAmburgh31-Aug-06 17:34
Rod VanAmburgh31-Aug-06 17:34 
AnswerRe: Bug with some buffer ? Pin
Rod VanAmburgh31-Aug-06 17:29
Rod VanAmburgh31-Aug-06 17:29 
GeneralMS-SQL server Self join Pin
Amit Malik25-Dec-05 19:08
Amit Malik25-Dec-05 19:08 
QuestionWorks great, 256 character limit? Pin
bertszoghy22-Apr-05 13:01
bertszoghy22-Apr-05 13:01 
AnswerRe: Works great, 256 character limit? Pin
Rod VanAmburgh22-Apr-05 16:54
Rod VanAmburgh22-Apr-05 16:54 
GeneralRe: Works great, 256 character limit? Pin
bertszoghy23-Apr-05 7:03
bertszoghy23-Apr-05 7:03 
GeneralRe: Works great, 256 character limit? Pin
bertszoghy25-Apr-05 2:49
bertszoghy25-Apr-05 2:49 
GeneralRe: Works great, 256 character limit? Pin
Rod VanAmburgh25-Apr-05 15:52
Rod VanAmburgh25-Apr-05 15:52 
GeneralRe: Works great, 256 character limit? Pin
Rod VanAmburgh25-Apr-05 17:31
Rod VanAmburgh25-Apr-05 17:31 
GeneralRe: Works great, 256 character limit? Pin
bertszoghy26-Apr-05 5:35
bertszoghy26-Apr-05 5:35 
GeneralRe: Works great, 256 character limit? Pin
Rod VanAmburgh26-Apr-05 14:37
Rod VanAmburgh26-Apr-05 14:37 
GeneralRe: Works great, 256 character limit? Pin
bertszoghy27-Apr-05 6:34
bertszoghy27-Apr-05 6:34 
Thanks Rod,

This works as advertised!

I'll take two.

Cheers,
Bert

Software Developer,
http://www.quadmore.com
GeneralCool Pin
Sebasthian3-Mar-05 17:36
Sebasthian3-Mar-05 17:36 
GeneralElaborate Pin
perlmunger7-Dec-04 5:07
perlmunger7-Dec-04 5:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.