.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> SQL Query >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

T-SQL code to send messages/notifications to all logged in users in Windows LAN

Posted By:Gowthammanju       Posted Date: June 28, 2011    Points: 40    Category: SQL Query    URL: http://www.dotnetspark.com  

This stored procedure accepts a message as an input parameter and sends that message to all the currently logged in SQL Server users using NET SEND.
 

Here it is


CREATE PROC notify_users
(
@notification VARCHAR(100) = 'SQL Server shutting down'
)
AS
BEGIN

/*******************************************************************************************************
Written by: G
owthammanjuDate written: November 26th 2000
Purpose: To send NET SEND messages to all the connected SQL Server users in an NT Local Arean Network

*******************************************************************************************************/
 
SET NOCOUNT ON
 
DECLARE @msg VARCHAR(250)
DECLARE @hostname sysname

SELECT @hostname= min(RTRIM(hostname))
FROM
  master.dbo.sysprocesses (NOLOCK)
WHERE
  hostname <> ''
 
WHILE @hostname is not null
    BEGIN
	set @msg='exec master.dbo.xp_cmdshell "net send ' + RTRIM(@hostname) + ' ' + RTRIM(@notification) + ' "'
        EXEC (@msg)
  	SELECT @hostname= min(RTRIM(hostname))
	FROM
  		master.dbo.sysprocesses (NOLOCK)
	WHERE
  		hostname <> ''
		and hostname > @hostname
    END
 
SET NOCOUNT OFF
 
END


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend