Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to send email of query results in tsql

Posted on 2008-10-24
4
1,503 Views
Last Modified: 2012-05-05
I have some tsql that performs a query to return the lastest DB backup info for a given backup(s).  I want to email the results of this query.  I have found some xp_send stuff, but wanted to get a whole solution.
0
Comment
Question by:66chawger
4 Comments
 
LVL 19

Accepted Solution

by:
folderol earned 400 total points
ID: 22801357
See these BOL articles.  You should be using sp_send_dbmail, which is the SQL2005 mail solution.
xp_sendmail is for older versions.

ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/f1d7a795-a3fd-4043-ac4b-c781e76dab47.htm
http://msdn.microsoft.com/en-us/library/ms175887.aspx


0
 
LVL 3

Expert Comment

by:raminhos
ID: 22801418
you can try something like this:

exec master.dbo.xp_sendmail
@recipients = 'myemail@email.com',
@subject = 'my email subject',
@message = 'My email message...',
@query = 'select * from table',
@attach_results = false

Open in new window

0
 
LVL 13

Assisted Solution

by:sm394
sm394 earned 100 total points
ID: 22802782
few more good article about setting up and sending database mail in sql server 2005
http://www.codeproject.com/KB/database/SQLServer2008DatabaseMail.aspx
http://www.mssqltips.com/tip.asp?tip=1438
0
 

Author Comment

by:66chawger
ID: 22822043
First of all, thanks for the info.  Attached is a code snippet (very crude) which performs a query and emails me the results.  What I want to do is (1) update the query so I can query for multiple DB's instead of duplicating the query multiple times  (2) Format the Query so the results line up with the headings (column names).  
EXEC sp_send_dbmail 
@recipients='me@mycompany.com',
@subject = 'DB Backups',
@body ='Please review latest SQL DB Backup files',
@query ='SELECT DISTINCT TOP 20
s1.type,
s1.backup_start_date,
s1.backup_finish_date,
--s1.first_lsn,
--s1.last_lsn,s1.checkpoint_lsn,
--s1.database_backup_lsn,
s2.physical_device_name
from msdb..backupset s1 inner join msdb..backupmediafamily s2
on s1.media_set_id = s2.media_set_id
where s1.database_name ="Database name 1" 
and s1.backup_start_date >= "10/10/2008 06:41:25 AM"
-- You will need to change to the database you are querying
and s1.type in("D","L","I") -- sl.type in ("D","L") means full or Transaction Log backups (if "I", Differential backups) or ("F" for File Group, "G" for File Group Differential)
and s1.backup_start_date >= (select max(backup_start_date) from msdb..backupset where database_name ="Database name 1" and type ="D") 
order by s1.backup_start_date desc'

Open in new window

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL help 5 56
Getting max record but maybe not use Group BY 2 32
Error in SSIS while executing  - Potential data loss 4 31
Loops and updating in SQL Query 9 54
There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question