Click here to Skip to main content
15,914,066 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.6K   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 
Hello,
This is urgent.
I have a Table ARTICLE_MASTER.
fields are item_code, activation_date.

I can different item_code for each article.
each article has a activation date.

select * from ARTICLE_MASTER
inner join
ARTICLE_MASTER ARTICLE_MASTER1
on ARTICLE_MASTER.item_code= ARTICLE_MASTER1.item_code
where ARTICLE_MASTER.activation_date < = getdate() and
ARTICLE_MASTER.end_date > = getdate() and
ARTICLE_MASTER.activation_date > ARTICLE_MASTER1.activation_date
order by article_master.item_code

I can have 4 rows for a single article - say 001- for Comb.
and for a given month say December, my acticle master can have 4 rows for article 001. with activation date and end date, for a single article i can have 4 rows.
say
ITEM_CODE               ACTIVATION_DATE            END_DATE        PRICE
001                     01/12/2005                 31/12/2005      15
001                     07/12/2005                 31/12/2005      20
001                     14/12/2005                 31/12/2005      25
001                     21/12/2005                 31/12/2005      20

SO After comparison with my current date i have to decide which price to apply.
say on 2nd dec price should be 15
on 8th dec price should be 20
on 15th dec price should be 25
on 21-22th dec price should be 20
so i need to pick the appropriate row for a given date.
The query i have written above gives only the latest one say on 16th still it gives me 21st dec's row.

Please give me a solution for it.
its urgent

Amit Malik

.NET Developer
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 
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.