T-SQL query result into variable - sql

I am trying to assign the result of a SQL query into a variable but running into issues.
Here is my query:
use db
SELECT name
,ID
,pID
,pName
,group
FROM mName as m
INNER JOIN pID_Check AS p ON m.ID= p.pID
WHERE p.pName NOT LIKE m.name
Query works fine however I'm trying to schedule it to run hourly via SQL Server Agent and email on results.
Most of the time the query will not return any data but in the event there are rows I need it to email out.
Essentially need values of the row added to #results variable and email triggered only if #results not null
SET #sub = 'Subject'
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'Profile',
#recipients = 'email#email.com;',
#body = #results,
#subject = #sub

You seem to want to send the result set to a mail recipient - while that is possible, I'm not aware of being possible to stick into the body in the method which you have attempted.
It can be included as an attachment via another property of SP_SEND_DBMAIL. Please see your example below, modified to include the result set as an attachment:
DECLARE #myquery varchar(max) = '
SELECT name
,ID
,pID
,pName
,group
FROM mName as m
INNER JOIN pID_Check AS p ON m.ID= p.pID
WHERE p.pName NOT LIKE m.name
'
SET #sub = 'Subject'
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'Profile',
#recipients = 'email#email.com;',
#body = #results,
#subject = #sub,
#query = #myquery,
#attach_query_result_as_file = 1,
#query_attachment_filename= 'MyFileName.csv';
All SP_SEND_DBMAIL properties can be seen here: https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-send-dbmail-transact-sql

Related

How to send an email only to users who appear in query results?

I am creating a query to return tickets that haven't been updated in the required time and send via email. I have everything working except I want to email only the users whose names appear in the query results.
I have tried setting the # user name = to user id and adding to the email as below. Not sure if this is even close.
Declare #username VARCHAR(MAX)
--("query")--
Set #username =[userid]--(in query results)--
exec msdb.dbo.sp_send_dbmail
#participants ='#username+#mydomain.com'
Expect to send emails to users whose user name appears in query results.
You can use STUFF with FOR XML PATH to create a ; delimited list for the #recipients parameter:
DECLARE #EmailList varchar(max)
SELECT #EmailList = STUFF((
SELECT ';' + Username + '#gmail.com'
FROM yourTable
FOR XML PATH('')
), 1, 1, '')
Then:
EXEC msdb.dbo.sp_send_dbmail
#recipients = #EmailList,
#subject = 'foo',
#body = 'bar',
etc
You can use Coalesce to create a list separated with semicolons which is what dbmail wants.
Declare #username varchar(max)
SELECT
#username = COALESCE(#username + '; ','') + userid + '#mydomain'
FROM
Yourtable
WHERE
Yourquery
EXEC msdb.dbo.sp_send_dbmail
#recipients = #username
#subject = 'BARK BARK'
#Body = 'BARK BARK BARK'
You can use a CURSOR to fetch your email address list and send the email to that list:
DECLARE #username AS VARCHAR(MAX) = ''
DECLARE #participants AS VARCHAR(MAX) = ''
DECLARE cursor_name CURSOR
FOR
SELECT userid
FROM YourTable
OPEN cursor_name;
FETCH NEXT
FROM cursor_name
INTO #username;
WHILE ##FETCH_STATUS = 0
BEGIN
#participants += #username + '#mydomain.com;'
FETCH NEXT FROM cursor_name
INTO #username;
END
CLOSE cursor_name;
DEALLOCATE cursor_name;
EXEC msdb.dbo.sp_send_dbmail #recipients = #participants
etc

Multiple table SQL trigger an email to update

Ok, much closer here for sure, but now it sends mass amounts of emails on one record update? I know I'm missing something here. First shot at cursors, so I'm guessing I've done something wrong in there? As always, Thanks for the help!!
create trigger eMailScheduleChange on dbo.BOOKINGS after update as
set nocount on;
Declare EmailCursor Cursor read_only for
select r.Name as RName
, c.Name as CName
, i.BookingTypeId
, i.Start
, i.Finish
, r.Email
from Wallchart.dbo.BOOKINGS b
inner join Wallchart.dbo.CUSTOMERS c on b.CustomerId = c.CustomerId
inner join Wallchart.dbo.RESOURCES r on b.ResourceId = r.ResourceId
inner join Inserted i on i.CustomerId = b.CustomerId
where i.BookingTypeId <> b.BookingTypeId
Declare #Email as varchar(50)
Declare #CName as varchar(100)
Declare #Start as datetime
Declare #RName as varchar (100)
Declare #Finish as datetime
Declare #body as varchar (255)
Declare #BookingTypeId as varchar (50)
open EmailCursor
Fetch next from EmailCursor
INTO #Email, #CName, #Start, #RName, #Finish, #BookingTypeId
While ##FETCH_STATUS=0
BEGIN
Set #body = '<Account cancelled>' + #CName + #Start + #Finish
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'SQLMail',
#recipients = #Email,
#subject = 'Account Update',
#body = #body
FETCH NEXT FROM EmailCursor INTO #Email, #CName, #Start, #RName, #Finish, #BookingTypeId
SET NOCOUNT OFF
END
Close EmailCursor
Deallocate EmailCursor
I think perhaps you have the impression that a query in a trigger is not allowed to read data from another table. This is not the case or triggers would be fairly useless. Here is a stub of how your trigger might look. I demonstrated how to pull the information you want. From there it is simply calling sp_send_dbmail.
create trigger MyTrigger on dbo.BOOKINGS for update as
set nocount on;
select r.Name
, c.Name
, i.BookingTypeId
, i.Start
, i.Finish
, r.Email
from Schedule.dbo.BOOKINGS b
inner join Wallchart.dbo.CUSTOMERS c on b.CustomerId = c.CustomerId
inner join Schedule.dbo.RESOURCES r on b.ResourceId = r.ResourceId
inner join Inserted i on i.SomeKeyValue = b.SomeKeyValue
where i.BookingTypeID <> b.BookingTypeID
--Now you can see how to capture all the information. All that is left is to call sp_send_dbmail
--https://msdn.microsoft.com/en-us/library/ms190307.aspx
SQL Server does not support direct mailing from the scripts. But you have other options.
First Create a table to capture the records that are being changed and also set a EmailFlag so that you can identify whether or not you have notified that user about the change.
The create a store procedure that will give you the list of all users that has undergone some changes but not notified by email (EmailFlag ="N" or something like that)
Now use the SSIS to create a send mail task to send the details email for all these users.
Once you have sent the email for each user, then you can update the EmailFlag as "Y" for that record. You can make the SSIS package a scheduled job in the SQL Server so that it will automatically run between certain interval.
You can use a for each loop container in SSIS so that you can send mail individually to each recipient
Please refer the following Links for more details
SSIS - How to configure a send mail task

Stored procedure sends email even when condition is false. Any ideas?

When a user uses our site to process certain requests, upon exiting the site, an email with a link goes out to that user asking him/her to click the link to take a survey and share his/her user experiences with us.
Below is the stored procedure that I have written that does as described above.
ALTER proc [dbo].[SendSurvey]
AS
BEGIN
Declare #sender nvarchar(200)
declare #dept nvarchar(200) = ''
declare #loc nvarchar(200) = ''
declare #dteCreated nvarchar
declare #RequestID nvarchar(50) = ''
declare #authorizedname nvarchar(200) = ''
declare #email nvarchar(200) = ''
declare #message nvarchar(1000) = ''
declare #mailid int = 0
SET QUOTED_IDENTIFIER OFF;
SELECT
#email = email, #mailid=ID, #message = #message,
#RequestID = RequestID,
#authorizedname = SUBSTRING(submittedBy, CHARINDEX(',', submittedBy) + 1, LEN(submittedBy) - CHARINDEX(',', submittedBy) + 1)
+ ' ' + SUBSTRING(submittedBy, 1, CHARINDEX(',', submittedBy) - 1),
#loc = Bldg, #dtecreated = DateCreated, #dept = Department
FROM
Survey
WHERE
email = #email
AND Email IS NOT NULL OR Email != ''
AND (orderStatus != 1)
SELECT #message = 'This is a computer generated email message.
Please DO NOT use the REPLY button above to respond to this email.
Dear '+ #authorizedname +':
Thank you for using the order processing system.
Please click the link below to complete a survey
http://feedbacksurvey.php?rID=' +#RequestID+'&loc='+Replace(#loc,' ', '%20')+'&dept='+Replace(#dept,' ', '%20')+'
Regards,
web admin.'
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'Customer Feedback Survey',
#recipients = #Email, -- your email
#subject = 'Customer Feedback Survey',
#body = #message;
UPDATE Survey
SET orderStatus = 1
WHERE orderStatus != 1 AND ID = #mailid
END
There are two problems with the stored procedure.
There is a column orderStatus which is a BIT data type with True (1) of false(0) value.
If the orderstatus is false, then send emails with records associated with it.
After sending the email, update orderstatus to true so the email doesn't get sent a second time.
This is not working. When I execute the stored procedure where all records on the table have orderstatus set to True, email still goes out.
the second problem that I am having is that the code is not sending out all records where orderStatus is True. It just sends email one at a time.
We would like emails to be send out for ALL records where orderstatus = 1 (True).
Any ideas what I am doing wrong?
You mixed AND with OR in your WHERE clause. The results will include all rows where Email != '', regardless of the other conditions.
Use parens to make this work:
WHERE email=#email
AND (Email IS NOT NULL or Email != '')
AND (orderStatus != 1)
As for why it's sending one email at a time, you are using your query to populate scalar variables.
SELECT #email = email...
Will result in #email being populated with one value, no matter how many rows the query returns.
the second problem that I am having is that the code is not sending out all records where orderStatus is True. It just sends email one at a time.
Yeah - that's how sp_send_dbmail works. You'll need a cursor to send > 1 email.
DECLARE c CURSOR LOCAL FAST_FORWARD READ_ONLY FOR
SELECT ....
FETCH NEXT FROM c INTO ....
WHILE ##FETCHSTATUS = 0 BEGIN
EXEC sp_send_dbmail ...
FETCH NEXT FROM c INTO ....
END
CLOSE c
DELLOCATE c

SQL Server 2005 Database Mail Failures

I am running database mail on a SQL 2005 box. Occasionally mails fail to send, by quering the msdb.dbo.sysmail_mailitems table i can see there are items with a sent_status of "2", which is failed. I can query the sysmail_faileditems table to list all failed mails.
Is there anyway i can process/re-send these failed mail's?
Would it be reasonable to create a daily job to query this table looping through using a CURSOR to re-send the mails one by one, and then delete them from the table one by one.
If you have a better suggestion / ideas then please let me know.
Many thanks Karl
First up, i suggest you query faileditems to determine your main cause of failure:
SELECT items.subject ,
items.last_mod_date ,
l.description
FROM dbo.sysmail_faileditems AS items
INNER JOIN dbo.sysmail_event_log AS l ON items.mailitem_id = l.mailitem_id
If it's nothing that can be easily fixed, you can re-send them by looping through the sysmail_mailitems table and re-sending them based on the failure type (timeouts etc) in the faileditems log - some good examples in the suggestions of this blog: http://justgeeks.blogspot.co.uk/2007/05/resending-sysmail-emails.html
My personal favourite:
CREATE PROCEDURE sysmail_resend_timeout
AS
BEGIN
SET NOCOUNT ON
DECLARE SYSMAIL_LOG_RESEND_CURSOR CURSOR READ_ONLY
FOR
SELECT DISTINCT
l.mailitem_id ,
p.name ,
m.recipients ,
m.subject ,
m.body_format ,
m.body
FROM msdb.dbo.sysmail_log l WITH ( NOLOCK )
JOIN msdb.dbo.sysmail_mailitems m WITH ( NOLOCK ) ON m.mailitem_id = l.mailitem_id
JOIN msdb.dbo.sysmail_profile p WITH ( NOLOCK ) ON p.profile_id = m.profile_id
WHERE l.event_type = 3
AND m.sent_status = 2
AND l.description LIKE '%The operation has timed out%'
ORDER BY l.mailitem_id
OPEN SYSMAIL_LOG_RESEND_CURSOR
WHILE ( 1 = 1 )
BEGIN
DECLARE #mailitem_id INT ,
#profile_name NVARCHAR(128) ,
#recipients VARCHAR(MAX) ,
#subject NVARCHAR(255) ,
#body_format VARCHAR(20) ,
#body NVARCHAR(MAX)
FETCH NEXT FROM SYSMAIL_LOG_RESEND_CURSOR INTO #mailitem_id, #profile_name, #recipients, #subject, #body_format, #body
IF NOT ##FETCH_STATUS = 0
BEGIN
BREAK
END
PRINT CONVERT(VARCHAR, GETDATE(), 121) + CHAR(9) + CONVERT(VARCHAR, #mailitem_id) + CHAR(9) + #recipients
EXEC msdb.dbo.sp_send_dbmail
#profile_name = #profile_name ,
#recipients = #recipients ,
#subject = #subject ,
#body_format = #body_format ,
#body = #body
UPDATE msdb.dbo.sysmail_mailitems
SET sent_status = 3
WHERE mailitem_id = #mailitem_id
END
CLOSE SYSMAIL_LOG_RESEND_CURSOR
DEALLOCATE SYSMAIL_LOG_RESEND_CURSOR
END
GO
I know it's not really the answer you want to hear, but I always try and decouple the mail feature. I might use a trigger to spawn an external process if the mail sending needs to be timely, but I let the external script do the actual job of sending the mail. That way transient connection errors are taken care of by the MTA, and I don't have to worry about special book-keeping algorithms.

Local variables in sp_send_dbmail?

I'm working on an SQL stored procedure that is supposed to send an attachment with the results of a query.
I am using sp_send_dbmail to send the email.
Within the query I'd like to send, I join the to a table variable. When I executed the stored procedure, I got an error message which said that the variable didn't exist.
My code:
DECLARE #t TABLE (
id INT IDENTITY(1,1),
some fields
)
DECLARE #query VARCHAR(MAX)
SET #query = 'SELECT
some values
FROM #t t
INNER JOIN dbo.Table d ON t.field = d.field
EXEC msdb.dbo.sp_send_dbmail #recipients=#recipients_list,
#subject = #subject,
#query = #query,
#attach_query_result_as_file = 1,
#query_result_width = 4000,
#query_attachment_filename = 'Details.txt'
Is there any way for me to refer to the local variable within this stored procedure? If not, why not?
TIA!
(I am using SQL Server 2005)
The query runs in a different context than your original code body, so it is not aware of any local variables. Try using a global temp table instead.
CREATE TABLE ##t (
id INT IDENTITY(1,1),
some fields
)
DECLARE #query VARCHAR(MAX)
SET #query = 'SELECT
some values
FROM ##t t
INNER JOIN dbo.Table d ON t.field = d.field'
EXEC msdb.dbo.sp_send_dbmail #recipients=#recipients_list,
#subject = #subject,
#query = #query,
#attach_query_result_as_file = 1,
#query_result_width = 4000,
#query_attachment_filename = 'Details.txt'
DROP TABLE ##t