Wednesday, December 22, 2010

How to setup database mail in SQL Server 2005?

How to setup database mail in SQL Server 2005?

Here is a small code snippet for setting up database mail.

Please make sure to do following before setting up database mail

  1. Enable database mail in SQL Server Surface Area configuration by clicking on “Surface area configuration for features”


  1. Select server instance

  1. Click on database and enable database mail SPs



  1. Click on SQL mail and enable SQL Mail SPs



  1. Now you are all set for creating and managing database mail profiles

Here is a script to do it

-------------------------------------------------------------
--  Create a Database Mail profile, an SMTP account and
--  associates the account to the profile.
-- 
-------------------------------------------------------------
DECLARE @profile_name sysname,
        @account_name sysname,
            @description nvarchar(256),
        @SMTP_servername sysname,
        @email_address NVARCHAR(128),
          @display_name NVARCHAR(128);
-- Profile name. Replace with the name for your profile
        SET @profile_name = 'SQLMAIL';
-- Account information. Replace with the information for your account.
            SET @account_name = 'SQLMAIL';
            SET @SMTP_servername = 'smtp.MyCompany.com';
            SET @email_address = 'sqlsa@MyCompany.com';
        SET @display_name = 'SQLMAIL';
            SET @description  = 'Mail account for Production Maintenance.'

-- Verify the specified account and profile do not already exist.
IF EXISTS (SELECT * FROM msdb.dbo.sysmail_profile WHERE name = @profile_name)
BEGIN
  RAISERROR('The specified Database Mail profile SQLSA already exists.', 16, 1);
  GOTO done;
END;

IF EXISTS (SELECT * FROM msdb.dbo.sysmail_account WHERE name = @account_name )
BEGIN
 RAISERROR('The specified Database Mail account SQLSA already exists.', 16, 1) ;
 GOTO done;
END;

-- Start a transaction before adding the account and the profile
BEGIN TRANSACTION ;

DECLARE @rv INT;

-- Add the account
EXECUTE @rv=msdb.dbo.sysmail_add_account_sp
    @account_name = @account_name,
      @description = @description,
    @email_address = @email_address,
    @display_name = @display_name,
    @mailserver_name = @SMTP_servername;

IF @rv<>0
BEGIN
    RAISERROR('Failed to create the specified Database Mail account SQLSA.', 16, 1) ;
    GOTO done;
END

-- Add the profile
EXECUTE @rv=msdb.dbo.sysmail_add_profile_sp
    @profile_name = @profile_name,
      @description = @description;

IF @rv<>0
BEGIN
    RAISERROR('Failed to create the specified Database Mail profile SQLSA.', 16, 1);
      ROLLBACK TRANSACTION;
    GOTO done;
END;

-- Associate the account with the profile.
EXECUTE @rv=msdb.dbo.sysmail_add_profileaccount_sp
    @profile_name = @profile_name,
    @account_name = @account_name,
    @sequence_number = 1 ;

IF @rv<>0
BEGIN
    RAISERROR('Failed to associate the speficied profile with the specified account SQLSA.', 16, 1) ;
      ROLLBACK TRANSACTION;
    GOTO done;
END;

COMMIT TRANSACTION;

done:

GO

Where is my table?

Where is my table?

I find it really annoying that users create tables with same name in multiple databases in development and sometimes production environment. They one fine day they compare the tables in and find the data different. This creates a big problem for DBAs figuring out what all places table exists. So here is a small script to do just that

exec master.dbo.sp_msforeachdb
"USE [?]
select      db_name()
from  sysobjects
where name = 'mytable'
"

Tuesday, December 21, 2010

Find Deprecated Features in SQL Server


Few days back I was working on project which involved server migration from SQL 2005 to SQL 2008. I wanted to find out what code that I use will not be available in the next version of SQL. These features are called as “deprecated features”. Now MS provides all kind of documents and links to read about it. But being a DBA I want to run a query and see the results J

Following will help you do it when it comes to deprecated features

SELECT * FROM sys.dm_os_performance_counters WHERE object_name LIKE ('%deprecated%')

object_name
counter_name
instance_name
cntr_value
cntr_type
SQLServer:Deprecated Features 
Usage
ALTER LOGIN WITH SET CREDENTIAL
0
65792
SQLServer:Deprecated Features
Usage
SQL_AltDiction_CP1253_CS_AS
0
65792
SQLServer:Deprecated Features
Usage
Macedonian
2
65792
SQLServer:Deprecated Features
Usage
Lithuanian_Classic
2
65792


How to read the results

object_name
Category to which this counter belongs
counter_name
Name of counter
instance_name
Name of the specific instance of the counter. Often contains the database name.
cntr_value
Current value of the counter.
cntr_type
Type of counter as defined by the Windows performance architecture.


How to send a CSV or XLS file as attachment using SQL Database mail with proper formatting


Today I encountered a weird (may be not) problem…. One of my colleagues was asked to generate a CSV file and email it to someone. He was getting ready to create a SSIS package with data flow task and everything. I thought why to waste time doing this when there is functionality available in SQL Server. Here you go…

Now please remember that the parameter @query_result_separator = ' ' needs to be re-typed to TAB if you are copy pasting the code. When you copy and paste the formatting in SSMS resets it to something else and the formatting in CSV file is all messed up

DECLARE @sub VARCHAR(100)
DECLARE @qry VARCHAR(1000)
DECLARE @msg VARCHAR(250)
DECLARE @query NVARCHAR(1000)
DECLARE @query_attachment_filename NVARCHAR(520)

SELECT @sub = 'TEST XML ATTACHMENT'
SELECT @msg = 'Please refer to the attached spread sheet for the report.'
SELECT @query = ' SET NOCOUNT ON;
            Select top 10 * from master..sysobjects WITH(NOLOCK)  '

SELECT @query_attachment_filename = 'test.csv'

EXEC msdb.dbo.sp_send_dbmail
            @profile_name = 'SQLMAIL',
            @recipients = 'sqldba@sqldba.com',
            @copy_recipients = 'sqldba@sqldba.com',
            @body = @msg,
            @subject = @sub,
            @query = @query,
            @query_attachment_filename = @query_attachment_filename,
            @attach_query_result_as_file = 1,
            @query_result_header = 1,
            @query_result_width = 256 ,
            @query_result_separator = '   ' ,
            @query_result_no_padding =1;