I need to send an email from SQL Server when a query result set holds records. The query can be based on a lot of logic with joins between several tables.
Please send me in the right direction (views, triggers on views, SQL Server agent job..?).
using sp_send_DBmail as documented here(https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-send-dbmail-transact-sql?view=sql-server-2017) for all parameter options
declare #bodytext varchar(max)= '<b>Hey look I wrote something</b>'
if(Exists(select 1 from ....))
begin
EXEC msdb.dbo.sp_send_dbmail
#recipients='xyz#gmail.com',
#subject='ATTN! There are records',
#body=#bodytext,
#body_format='HTML',
#from_address='DBA <kl#domain.com>',
#reply_to='xyz#gmail.com'
end
take a look at Vsql-email app (you can search on google, I think I'm not allowed to post the direct link here), it has this option to not send the email if the query has 0 rows, and you can send the email as HTML formatted body and/or as excel attachment and no need to enable and configure Database Mail in SQL Server and write code for HTML formatting.
Related
We have an email that is being sent to us from SQL Server that we want to stop. How do I find out which stored procedure is sending that email so I can modify/disable it?
What I've tried
Searching through all stored procedures via sys.all_sql_modules for words in the email subject and body text.
SELECT *
FROM sys.all_sql_modules AS asm
INNER JOIN sys.objects AS o ON o.object_id = asm.object_id
WHERE asm.definition LIKE '%*[subject/body words]*%'
I've looked through the sysmail_* tables and I have found my email being sent but I haven't found a way to trace it back to a stored procedure.
SELECT TOP 100 *
FROM msdb.dbo.sysmail_sentitems AS ss
WHERE ss.subject LIKE '%keyword%'
you could search sys.modules for the string
sp_send_dbmail
Then look at all the hits. OTOH how do you know it was sent from a PROC at all? It could be an ad-hoc query calling sp_send_dbmail, or an agent job, or something else.
I would simply create a server side trace and with filter for textdata like '%sp_send_dbmail%'
Then just run the trace for 1 day, and then you can figure out which SP is sending out db mail. This way, you can surely find out whether the email is sent via a stored proc or via an ad-hoc query (like inside a SSRS report?) or from a job step.
I have setup a report that uses a stored procedure to create the dataset. I am sending this report to 4 users using an email subscription on a daily basis. More often than not, the report will not have any data. How do I get the subscription to only send the email when the report has data?
Anyone else who stumbles across this, you can try this little trick. It will throw an error, which will prevent the report from being sent.
IF ##ROWCOUNT = 0 RAISERROR('No Results', 16, 1);
Use a data-driven report, if you're on Enterprise edition. If not, you'll need to see if any rows were returned from the SP first, and only execute the subscription if there are. This is pseudo-SQL, in the absence of a lot of context, however, something like:
IF EXISTS(SELECT 1 FROM YouTable JOIN YourOtherTable WHERE ...) BEGIN
EXEC ReportServer.dbo.AddEvent #EventType='TimedSubscription', #EventData='a6c151ca-ff47-46c0-b807-ad1ac8116769';
END
I have recently started using SQL Server 2016 and I'm using SESSION_CONTEXT values to pass some data around.
I'm trying to find out if there is any way to read all session context settings in one. To clarify this is for debugging purposes only - I can already access individual settings, (see code below). I would like to be able to read all such settings in one go if possible.
-- What I Have
EXEC sp_set_session_context 'SortOrder','Price ASC'
EXEC sp_set_session_context 'ItemsPerPage',20
SELECT SESSION_CONTEXT(N'SortOrder') [SortOrder]
SELECT SESSION_CONTEXT(N'ItemsPerPage') [ItemsPerPage]
SELECT SESSION_CONTEXT(N'NotSetYet') [NotSetYet]
-- What I'd like
SELECT * FROM SESSION_CONTEXT_TABLE
Any help gratefully received.
Many Thanks.
Session Context Stored in System pages so you can not retrieve the list of all context without having the key.
If you are looking for global parameters try to save as JSON, XML or CSV string and retrieve with the key.
So after some research I figured out that Standard edition does have the ability to manage data-driven subscriptions, so you'd have to write a custom script or stored procedure to work around it and get the same result.
My goal is be able to edit our existing reports (most of them are done with SSRS but we have a number of them created as SQL Server Agent jobs) so that they only e-mail the report if data is available; if no rows come up I want it to cancel sending the e-mail.
Before tackling an existing report, I tried creating a simple test script to get a better understanding DB Mail and stored procedures, so I came up with this script:
IF Exists ( Select cht_number, cht_itemcode, cht_description from chargetype where last_updatedate>'11/11/2014')
execute msdb.dbo.sp_send_dbmail
#profile_name=Null,
#recipients='email#company.com',
#subject='Test',
#Execute_Query_Database='DB_Name',
#query='Select cht_number, cht_itemcode, cht_description from chargetype where last_updatedate>''11/11/2014''',
#attach_query_result_as_file=1,
#query_attachment_Filename='TEST.csv',
#query_result_no_padding=1,
#query_result_header = 1,
#query_result_width = 256,
#query_result_separator=' '
IF ##ROWCOUNT = 0 raiserror ('No Data', 16, 1)
To test this script, I would edit something in that table so that only the most recent items would be sent in the report. If no data was available, it would just raise the error "No Data".
Can anyone suggest another way of getting this result, or how I could shorten this and be able to use it sort of like a template I could fiddle with to fit in an existing script?
How can i send email if execute sql task get executed and loads a table. so, if table is loaded with any record send email, if not loaded no email.
Appreciate any help.
after that task add another task that checks if there are any rows in the table
something like
IF EXISTS (SELECT 1 FROM Yourtable)
BEGIN
EXEC msdb.dbo.sp_send_dbmail ---------
END
read up on sp_send_dbmail here: http://msdn.microsoft.com/en-us/library/ms190307.aspx
its cool i solved it. i used two execute sql tasks and first one for loading data into the table, second one counting the records and i put variable on green arrow #MyVariable > 0 and connected the send mail task.
Thanks to all.