woensdag 19 november 2008

Email from SQL Server 2000

Add Extend procedure:
====================
xp_smtp_sendmail
---------------------------------
USE [BeheerDB]
GO
/****** Object: Table [dbo].[DBA_sysdatabases] Script Date: 11/19/2008 16:58:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[DBA_sysdatabases](
[name] [sysname] NOT NULL,
PRIMARY KEY CLUSTERED
(
[name] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
---------------------------------
USE [BeheerDB]
GO
/****** Object: Table [dbo].[DBA_syslogins] Script Date: 11/19/2008 16:58:24 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[DBA_syslogins](
[name] [sysname] NOT NULL,
PRIMARY KEY CLUSTERED
(
[name] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

-----------------------------------
if not exists (select 1 from tempdb..sysobjects
where type = 'U' and name = 'DBA_syslogins')
create table tempdb..DBA_syslogins
(name sysname not null primary key)
go
declare @body varchar(8000)
set @body = 'New logins: '

-- If it's not the first time it executes:
if exists (select 1 from tempdb..DBA_syslogins)
begin
-- If there are any new logins:
if exists (select 1 from master..syslogins
where name not in
(select name from tempdb..DBA_syslogins))
begin
select @body = @body + name + ','
from master..syslogins
where name not in (select name from tempdb..DBA_syslogins)

-- Omit the last comma:
select @body = substring(@body,1,LEN(@body)-1)

-- Send the message to the DBA with new logins:
EXEC msdb.dbo.sp_send_dbmail
@recipients=N'michelle.gutzait@Adventure-Works.com',
@body=N'New logins have been created.'
end
end

-- Modify the table’s content anyway
-- (since we’re not checking for deleted logins):
truncate table tempdb..DBA_syslogins
insert into tempdb..DBA_syslogins (name)
select name from master..syslogins

----------------------------------
USE [BeheerDB]
GO
/****** Object: StoredProcedure [dbo].[spDBA_NewDatabaseNotification] Script Date: 11/19/2008 15:21:23 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER proc [dbo].[spDBA_NewDatabaseNotification]
As

if not exists (select 1 from BeheerDB..sysobjects where type = 'U' and name = 'DBA_sysdatabases')
create table BeheerDB..DBA_sysdatabases
(name sysname not null primary key)
declare @body varchar(8000)
set @body = ',New databases: '

-- If it's not the first time it executes:
if exists (select 1 from BeheerDB..DBA_sysdatabases)
begin
if exists (select 1 from master..sysdatabases
where name not in
(select name from BeheerDB..DBA_sysdatabases))

-- Modify the table’s content anyway
-- (since we’re not checking for deleted databases):
truncate table BeheerDB..DBA_sysdatabases
insert into BeheerDB..DBA_sysdatabases (name)
select name from master..sysdatabases


begin
select @body= @body + name + ','
from master..sysdatabases
where name not in
(select name from BeheerDB..DBA_sysdatabases)

-- Omit the last comma:
select @body = substring(@body,1,LEN(@body)-1)

-- Send the message to the DBA with new databases:
exec master.dbo.xp_smtp_sendmail
@TO = N'bfadam@binck.nl',
@From = N'O1WSW00NL',
@priority = N'NORMAL',
@subject = @body,
@type = N'text/plain',
@message = N'New databases have been created',
@messagefile= N'',
@attachment = N'',
@attachments= N'',
@codepage = 0,
@timeout = 10000,
@server = N'EXCHCLUSTER.binck.nl'


end
end

Geen opmerkingen:

Een reactie posten