Thursday 29 December 2022

SQL Server : When was my SQL Server Database Last Accessed?


Your client come up to you and says , “Hey, I wanna list of all of our databases and when they were last accessed”.
You might be okay if you have auditing enabled or if there is a trace or xevent capturing this kind of data, but I'm guessing none of them exist. That's great, I don't usually monitor things like that either.

Is there a method for you to obtain that information if you're not keeping an eye on it? Nothing particularly clear comes from SQL; there is no magic in the "last_access_date" column in sys.databases or anywhere else for that matter.

It's possible to work around this issue; have a look at sys.dm_db_index_usage_stats. For every index in a database, you will observe the following intriguing columns: last_user_seek, last_user_scan, last_user_lookup, and last_user_update.
You may find out when the database was last visited by using the code below, which will provide the most recent date that any index in the database was modified.
SELECT db_name(databases.database_id) AS DBName,
(SELECT MAX(last_user_access)
FROM (VALUES (MAX(last_user_seek)),(MAX(last_user_scan))
,(MAX(last_user_lookup))) AS value(last_user_access)) AS last_user_access
FROM sys.dm_db_index_usage_stats indexstats
RIGHT OUTER JOIN sys.databases databases
ON indexstats.database_id = databases.database_id
GROUP BY databases.database_id
ORDER BY db_name(databases.database_id)
Note: sys.dm_db_index_usage_stats like many DMVs is cleared out on a SQL restart.

Thursday 22 December 2022

RDS SQL Server : Enable and Configure Database mail profile


In this article will discuss about enabling and configuring Database Mail on AWS RDS SQL Server.

Before configuring Database Mail, you've to first enable it through a database parameter group.
In Amazon RDS, parameter groups act as a container for engine configuration values that are applied to one or more DB instances. Each RDS instance comes with an associated default parameter group; however, we can’t modify it.

You can either use a new parameter group or an existing created parameter group. If choosing an existing parameter group, it must support your SQL Server instance edition and version.
To enable Database Mail through a parameter group, complete the following steps:

- On the Amazon RDS console, choose Parameter groups.
- Choose the parameter group you want to use.
- In the search box, enter database mail xps.
- Choose Edit Parameters to modify the value.
- For Values, choose 1.
- Save your changes.
- On the Amazon RDS console, choose Databases.
- Choose the instance you want to use.
- Choose Modify.
- Under Database options, choose the parameter group that has database mail xps set to 1.
- Choose Continue.
- Under Scheduling of modifications, choose Immediately.
- Choose Modify DB Instance to apply the changes.

Before we can use Database Mail, we need to set up a mail configuration.
- Launch SQL Server Management Studio.
- Connect to the SQL Server engine of the RDS instance that Database Mail is enabled for.
- Open a new query.
Use the following stored procedures to create a simple Database Mail configuration.

- Create a Database Mail profile (a profile is a container used to store email accounts). See the following code:
use msdb
go

EXECUTE msdb.dbo.sysmail_add_profile_sp
@profile_name = 'Notifications',
@description = 'Profile used for sending outgoing notifications using SES.' ;

- Add principles to the profile; use public so any user can access the profile:
use msdb
go

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

We can grant the permissions on Database Mail objects as needed, but public is fine for now.
- Create the Database Mail account (make sure to enter the correct SMTP credentials):

use msdb
go

EXECUTE msdb.dbo.sysmail_add_account_sp
@account_name = 'Acc1',
@description = 'Mail account for sending outgoing notifications.',
@email_address = 'example@example.com',
@display_name = 'Automated Mailer',
@mailserver_name = 'email-smtp.us-west-2.amazonaws.com',
@port = 587,
@enable_ssl = 1, @username = 'SMTP-username',
@password = 'SMTP-password' ;

- Add Database Mail account to the Database Mail profile:
use msdb
go

EXECUTE msdb.dbo.sysmail_add_profileaccount_sp
@profile_name = 'Notifications',
@account_name = 'Acc1',
@sequence_number = 1 ;

- Sending a test email
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'Notifications',
@recipients = 'success@simulator.amazonses.com',
@body = 'The database mail configuration was completed successfully.',
@subject = 'Automated Success Message';
GO

Then run this stored procedure to see all email items:
SELECT * FROM msdb.dbo.sysmail_allitems

Please refer the below document for more details on the same : [+] Using Database Mail on Amazon RDS for SQL Server; https://aws.amazon.com/blogs/database/using-database-mail-on-amazon-rds-for-sql-server/