0

In the database i manage, we have some internal dossiers that have a dead line, stablished by us on the date of creation. Now, i need to receive an e-mail, telling me which one have terminated.

I have created a simple job with code that would do it all, no go. I have created an alert, no go, always getting errors or, when not geting errors, nothing happens anyway.

select *
from bo
where nmdos like '%preço%'
  and datafinal = DATEADD(day, -1, convert(date, GETDATE()))

i need to create an alert that, when the code returns the name and the client number on that dossier

6
  • 1
    What does "no go" mean? That doesn't sound like a SQL Server error.
    – Thom A
    Feb 7, 2019 at 10:33
  • 1
    @Larnu RAISERROR('no go'), obviously Feb 7, 2019 at 10:35
  • no go means it didn't return anything, and i created a specific entry to test it. if i run the code as a simple query it returns what it should, when on a job it doesn't do anything Feb 7, 2019 at 10:48
  • But that SQL, on it's own, won't send an email; so I wouldn't expect an email to be sent.
    – Thom A
    Feb 7, 2019 at 11:01
  • @questionador you didn't post any email-related code. You didn't post the job creation SQL either. The way SQL Server Agent works hasn't changed. Changed SQL Server version though suggest you have a different machine, with a different configuration from whatever you used in the past. Maybe email isn't even enabled. Maybe the agent isn't running. Maybe the account it uses can't read from the table. Feb 7, 2019 at 11:15

1 Answer 1

-1

You could create stored procedure to handle the query check, which would check if any rows are returned. If there are rows returned then send an email. This can be achieved by the below. Assuming you have dbsend mail configured in your SQL instance.

 CREATE PROCEDURE dbo.DossierEmailSend
 AS
 DECLARE @rows int;
 DECLARE @message varchar(1000);
 SET @rows = (SELECT COUNT(*)  
            FROM bo
            WHERE nmdos LIKE '%preço%'
            AND datafinal = DATEADD(day, -1, CONVERT(date, GETDATE()))
            )
 SET NOCOUNT ON 
 SET @message = '<HTML>As at ' + CONVERT(char(19),GETDATE(),120) + '<BR><BR>DOSSIER FOUND'

 IF @rows > 0 
 BEGIN

 EXEC dbo.uspSendEmail 'FOUND DOSSIER', 'YOUR EMAIL', @message, NULL, 'CC EMAIL 1; CC EMAIL 2'
 SET NOCOUNT OFF

 END

You can then create a job within SQL Agent that will execute the check on whatever frequency you require.

If you are missing the send mail sp you can create it:

 CREATE proc [dbo].[uspSendEmail]
 @subject nvarchar(max),
 @to nvarchar(max),
 @body nvarchar(max),
 @file nvarchar(max)='',
 @cc nvarchar(max)='',
 @bcc nvarchar(max)='',
 @query nvarchar(max)='',
 @attach_query_result_as_file tinyint=0,
 @query_attachment_filename nvarchar(max)=''
 as      

 EXEC msdb.dbo.sp_send_dbmail @profile_name = NULL,
 @body_format = 'HTML',
 @copy_recipients = @cc,
 @blind_copy_recipients = @bcc,
 @recipients = @to,
 @body = @body,
 @subject = @subject,
 @file_attachments = @file,
 @query = @query, 
 @attach_query_result_as_file = @attach_query_result_as_file,
 @query_attachment_filename = @query_attachment_filename 
 return 0 
5
  • There's no uspSendEmail Feb 7, 2019 at 11:16
  • So create it - Answer updated to include send mail sp. don't mark down because you haven got the necessary stored procedure created or mail configured. The answer hold water.
    – jimmy8ball
    Feb 7, 2019 at 11:22
  • @jimmy8ball i'm almost afraid of asking this, because it will probably make me look silly, but creating that stored procedure (and i believe it will solve my question!) got an error, syntax error on declare@rows, any thoughts? Feb 7, 2019 at 11:23
  • Why create a new SP just to call msdb.dbo.sp_send_dbmail? Why not just call it directly?
    – Thom A
    Feb 7, 2019 at 15:41
  • @Larnu I have a secondary to reduce the extra code to be written in other scripts. But yes no argument at all, there is no reason you cannot just call directly.
    – jimmy8ball
    Feb 8, 2019 at 17:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.