CREATE PROC notify_users (
@notification VARCHAR(100) = 'SQL Server shutting down'
)
AS
BEGIN
/* Input parameters: Message to be sent */
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