Raghwendra mishra

Tuesday, 30 June 2015

Sending Mail using SQL Server Express Edition

SQL Server express edition,We either use CLR integration or configure SQL Mail using MSDB system database.
Here I am discussing sending mail using MSDB system database.By default the MSDB database installed when we install SQL Server. The below tables used to confiure sysmail account.

SELECT *FROM msdb.dbo.sysmail_account
SELECT *FROM msdb.dbo.sysmail_configuration
SELECT *FROM msdb.dbo.sysmail_principalprofile
SELECT *FROM msdb.dbo.sysmail_profile
SELECT *FROM msdb.dbo.sysmail_profileaccount
SELECT *FROM msdb.dbo.sysmail_profileaccount

First of all enable Database Mail XPs through below code to configure database mail.
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Database Mail XPs', 1;
GO
RECONFIGURE
GO

And then to configure SQL mail we need to follow below steps.


1. Use sysmail_add_account_sp stored procedure of MSDB database to configure sysmail account.

EXECUTE msdb.dbo.sysmail_add_account_sp
@account_name = 'DBA',
@description = 'Sent Mail using MSDB',
@email_address = 'raghwendra.mishra@spluspl.com',
@display_name = 'DBA',
@username='raghwendra.mishra@spluspl.com',
@password='Admin@123',
@mailserver_name = 'smtp.Your Mail server.com'

2. Use sysmail_add_profile_sp stored procedure of MSDB database to configure Database Profile.

EXECUTE msdb.dbo.sysmail_add_profile_sp
@profile_name = 'DBA',
@description = 'Profile used to send mail'

3. Use sysmail_add_profileaccount_sp stored procedure of MSDB database to map database mail account to Profile.

EXECUTE msdb.dbo.sysmail_add_profileaccount_sp
@profile_name = 'DBA',
@account_name = 'DBA',
@sequence_number = 1





4. To Grants permission for a database user or role to use a Database Mail profile use sysmail_add_principalprofile_sp

EXECUTE msdb.dbo.sysmail_add_principalprofile_sp
@profile_name = 'DBA',
@principal_name = 'public',
@is_default = 1 ;


5.   Send Mail using Created Profile.
exec msdb.dbo.sp_send_dbmail @profile_name = 'DBA', 
@recipients = 'raghwendra.mishra@spluspl.com', 
@subject = 'Mail Test', 
@body = 'Mail Sent Successfully', 
@body_format = 'text'

After executing above query check your email Inbox you will get mail like:

Friday, 12 June 2015

Server Server RPO and RTO

Recovery Point Objective (RPO) describes a point in time that data can be restored from. 
For instance, if there is data corruption, Data loss, at what point in time can a valid copy of the data be restored from? RPO is usually measured as minutes, hours, days, weeks, etc

If a database is corrupted then can we restore it back to what it was 30 minutes ago? This would be an RPO of 30 minutes.







Recovery Time Objective (RTO) is the amount of time which data or hardware is desired to be restored after a data corruption or hardware failure. 

If a Database server just died. Time needed to bring it up and running again? If the answer is two hour then the Recovery Time Objective is two hour.

In a condition when a calamity occurs, the only main thing is to be bring the application available to users. There are many ways to make sure the applications can be brought back online like

1- Offline backup restoration 
2- By using online replication method -- method to be available in case of Disaster
3- By using Mirroring /log shipping method – method to be available in case of Disaster

RTO and RPO needs to be defined with SLA(Service Level Agreement) to define the amount of data loss bearable in case of disaster and how much time is tolerable to get the services up and running.

Thursday, 11 June 2015

DBCC Commands

DBCC (Database consistency checker) are used to check the consistency of the databases. The DBCC commands are most useful for performance and troubleshooting exercises.
I have listed down and explained all the DBCC commands available in SQL Server, with examples.
The DBCC Commands broadly falls into four categories:


1- Maintenance Commands Performs maintenance tasks on a database, index, or file group.

1. CLEANTABLE - Reclaims space from the dropped variable-length columns in tables or index views.
DBCC CLEANTABLE ('AdventureWorks','Person.Contact', 0)
2. DBREINDEX - Builds one or more indexes for the table in the specified database.
(Will be removed in the future version, use ALTER INDEX instead) 
USE AdventureWorks
DBCC DBREINDEX ('Person.Contact','PK_Contact_ContactID', 80)
3. DROPCLEANBUFFERS - Removes all clean buffers from buffer pool.
DBCC DROPCLEANBUFFERS
4. FREEPROCCACHE - Removes all elements from the procedure cache
DBCC FREEPROCCACHE
5. INDEXDEFRAG - Defragments indexes of the specified table or view.
DBCC INDEXDEFRAG ('AdventureWorks', 'Person.Address', PK_Address_AddressID)
6. SHRINKDATABASE - Shrinks the size of the data and log files in the specified database
DBCC SHRINKDATABASE ('AdventureWorks', 10)
7. SHRINKFILE - Shrinks the size of the specified data or log file for the current database or empties a file by moving the data from the specified file to other files in the same filegroup, allowing the file to be removed from the database.
USE AdventureWorks;
- Shrink the truncated log file to 1 MB.
DBCC SHRINKFILE (AdventureWorks_Log, 1)
8. UPDATEUSAGE - Reports and corrects pages and row count inaccuracies in the catalog views.
DBCC UPDATEUSAGE (AdventureWorks)


2- Informational Commands Performs tasks that gather and display various types of information.

1. CONCURRENCYVIOLATION - is maintained for backward compatibility. It runs but returns no data.
DBCC CONCURRENCYVIOLATION
2. INPUTBUFFER - Displays the last statement sent from a client to an instance of Microsoft SQL Server 2005.
DBCC INPUTBUFFER (52)
3. OPENTRAN - Displays information about the oldest active transaction and the oldest distributed and nondistributed replicated transactions, if any, within the specified database.
DBCC OPENTRAN;
4. OUTPUTBUFFER - Returns the current output buffer in hexadecimal and ASCII format for the specified session_id.
DBCC OUTPUTBUFFER (52)
5. PROCCACHE - Displays information in a table format about the procedure cache.
DBCC PROCCACHE
6. SHOW_STATISTICS - Displays the current distribution statistics for the specified target on the specified table 
USE AdventureWorks
DBCC SHOW_STATISTICS ('Person.Address', AK_Address_rowguid)
7. SHOWCONTIG- Displays fragmentation information for the data and indexes of the specified table or view.
USE AdventureWorks
DBCC SHOWCONTIG ('HumanResources.Employee');
8. SQLPERF - Provides transaction log space usage statistics for all databases. It can also be used to reset wait and latch statistics.
DBCC SQLPERF (LOGSPACE)
9. TRACESTATUS - Displays the status of trace flags.
DBCC TRACESTATUS (-1)
10. USEROPTIONS - Returns the SET options active (set) for the current connection.


3- DBCC USEROPTIONS Validation Commands Performs validation operations on a database, table, index, catalog, filegroup, or allocation of database pages.

1. CHECKALLOC - Checks the consistency of disk space allocation structures for a specified database.
DBCC CHECKALLOC (AdventureWorks)
2. CHECKCATALOG - Checks for catalog consistency within the specified database.
DBCC CHECKCATALOG (AdventureWorks)
3. CHECKCONSTRAINTS - Checks the integrity of a specified constraint or all constraints on a specified table in the current database.
DBCC CHECKCONSTRAINTS WITH ALL_CONSTRAINTS
4. CHECKDB - Checks the logical and physical integrity of all the objects in the specified database.
DBCC CHECKDB (AdventureWorks)
5. CHECKFILEGROUP - Checks the allocation and structural integrity of all tables and indexed views in the specified filegroup of the current database.
USE AdventureWorks
DBCC CHECKFILEGROUP
6. CHECKIDENT - Checks the current identity value for the specified table and, if it is needed, changes the identity value.
USE AdventureWorks;
DBCC CHECKIDENT ('HumanResources.Employee')
7. CHECKTABLE - Checks the integrity of all the pages and structures that make up the table or indexed view.
USE AdventureWorks;
DBCC CHECKTABLE ('HumanResources.Employee')

4- Miscellaneous Commands Performs miscellaneous tasks such as enabling trace flags or removing a DLL from memory.

1. Dllname (FREE) - Unloads the specified extended stored procedure DLL from memory.
DBCC xp_sample (FREE)
2. TRACEOFF - Disables the specified trace flags.
DBCC TRACEOFF (3205)
3. HELP - Returns syntax information for the specified DBCC command.
   - List all the DBCC commands
DBCC HELP ('?')
- Show the Syntax for a given DBCC command
DBCC HELP ('checkcatalog')
 4. TRACEON - Enables the specified trace flags.
DBCC TRACEON (3205)

Tuesday, 9 June 2015

SQL SERVER 2008 – Database Mail Configuration – Send Email From SQL Database

This post will help to configure Database Mail which is used to send the Email using SQL Server. In order to send mail using Database Mail in SQL Server, there are 3 basic steps that need to be carried out.  
1. Create Profile and Account 
2. Configure Email 
3. Send Email.
Step 1) Create Profile and Account:
We need to create a profile and account using the Configure Database Mail Wizard which can be accessed from the Configure Database Mail in Management Node. This wizard is used to manage accounts, profiles, and Database Mail global settings which are shown below:
Please follow the snapshots in sequence :-

Step 2) Configure Email:
After the Account and the Profile are created successfully, we need to configure the Database Mail. To configure it, we need to enable the Database Mail XPs parameter through the sp_configure stored procedure, as shown here:

SP_CONFIGURE 'show advanced', 1
GO
RECONFIGURE
GO
SP_CONFIGURE 'Database Mail XPs',  1
GO
RECONFIGURE
GO

Step 3) Send Email:
After all configurations are done, we are now ready to send an email. To send mail, we need to execute a stored procedure sp_send_dbmail and provide the required parameters as shown below:
USEmsdb
GO
EXEC sp_send_dbmail @profile_name='demoprofile',
@recipients='raghwendra.mishra@spluspl.com',
@subject='Test mail--Demo mail configuration,
@body='This post will help to configure Database Mail which is used to send the Email using SQL Server. In order to send mail using Database Mail in SQL Server, there are 3 basic steps that need to be carried out.  
1.Create Profile and Account 
2.Configure Email 
3.Send Email. '