Tuesday, May 14, 2013

How to resend a message using dbmail

I'm a good DBA, developer, and all around wonderful human being. That means I love sets, hate cursors, and document my databases in my spare time.

However.

In this life, we all find strange things that can only be done one way. The below script is, I think, one of those unfortunate examples of that, in that it uses a cursor to get work done. However, if you can find a better way, email it or post a comment, and let's have a discussion.

Here's the short version:

I use SQL Agent to run a bunch of jobs overnight. SQL Agent is configured to send me the job status at the end of the job, succeed or fail.

In my situation, I needed to get all the failed messages for some time period to audit them. In order to get SQL Server dbmail to re-send the messages, I couldn't find an easy way to do this, so I made my own way to force the messages that weren't sent to get resent.

This is one of those cases where cursors make sense, because essentially, this is dynamic sql, due to the requirement to call the SP with the parameter values that change with every iteration of the loop.

I don't like being forced to use a cursor, but it works, it did indeed resend the emails, and it let me audit what I needed.



USE msdb;
go

DECLARE c1 CURSOR FOR
  SELECT DISTINCT
  f.recipients
  ,
  f.subject,
  f.body,
  f.importance,
  f.sensitivity
  FROM   msdb.dbo.sysmail_faileditems AS f
         JOIN msdb.dbo.sysmail_sentitems AS s
           ON f.profile_id = s.profile_id
              AND Isnull(f.recipients, '') = Isnull(s.recipients, '')
              AND Isnull(f.copy_recipients, '') = Isnull(s.copy_recipients, '')
              AND Isnull(f.blind_copy_recipients, '') =
                  Isnull(s.blind_copy_recipients, '')
              AND f.importance = s.importance
              AND f.sensitivity = s.sensitivity
  WHERE  Datediff(dd, f.sent_date, Getdate()) < 31
         AND f.body <> s.body;

DECLARE @Failed_profile_name                SYSNAME,
        @Failed_recipients                  VARCHAR(max),
        @Failed_copy_recipients             VARCHAR(max),
        @Failed_blind_copy_recipients       VARCHAR(max),
        @Failed_from_address                VARCHAR(max),
        @Failed_reply_to                    VARCHAR(max),
        @Failed_subject                     NVARCHAR(255),
        @Failed_body                        NVARCHAR(max),
        @Failed_body_format                 VARCHAR(20),
        @Failed_importance                  VARCHAR(6),
        @Failed_sensitivity                 VARCHAR(12),
        @Failed_file_attachments            NVARCHAR(max),
        @Failed_query                       NVARCHAR(max),
        @Failed_execute_query_database      SYSNAME,
        @Failed_attach_query_result_as_file BIT,
        @Failed_query_attachment_filename   NVARCHAR(255),
        @Failed_query_result_header         BIT,
        @Failed_query_result_width          INT,
        @Failed_query_result_separator      CHAR(1),
        @Failed_exclude_query_output        BIT,
        @Failed_append_query_error          BIT,
        @Failed_query_no_truncate           BIT,
        @Failed_query_result_no_padding     BIT
;

OPEN c1;

FETCH next FROM c1 INTO @Failed_recipients, @Failed_subject, @Failed_body,
@Failed_importance, @Failed_sensitivity;

WHILE @@fetch_status = 0
  BEGIN
      FETCH next FROM c1 INTO @Failed_recipients, @Failed_subject, @Failed_body,
      @Failed_importance, @Failed_sensitivity;

      EXEC Sp_send_dbmail
        @profile_name = 'Default Profile',
        @recipients = @Failed_recipients,
        @from_address = 'era@HQ.DHS.GOV',
        @reply_to = 'era@HQ.DHS.GOV',
        @subject = @Failed_subject,
        @body = @Failed_body,
        @importance = @Failed_importance,
        @sensitivity = @Failed_sensitivity;
  END

CLOSE c1;

DEALLOCATE c1; 


Tuesday, May 7, 2013

error code 84bc0643

SQL Server 2012 install of SP1 fails with error code 84BC0643.

"Windows Update ran into a problem."

No kidding, Windows Update. 

This was followed by the Windows Update (KB2793634) - "Windows Installer starts repeatedly after you install SQL Server 2012 SP1"

This update DID succeed, and Windows Update now shows a clean slate - no updates found.


Tuesday, April 23, 2013

Ctrl+R doesn't work

I had the following conversation with my VM the other day:

Why can't I push Control+R (Ctrl+R) in SSMS 2012, and have my query results window go away?  
I'm on a virtual machine here, Mr. SSMS, and I really don't need to see, again, that I just accidentally selected getdate().

In order to get Management Studio to give me back my full editing window, I went in to settings and reset the keyboard layout.

Like this:


  1. Click Tools >> Options
  2. Click Environment >> Keyboard
  3. Click the Reset button
  4. Answer Yes to the warning
  5. Click OK to close the window
  6. Exit SSMS
  7. Start SSMS again
  8. Select getdate();
  9. Ctrl+R now works!

Tuesday, April 16, 2013

MS SSIS Wiki

You know, it's sites like this that make me wish I had a page crawler, that would self-organize a website into an eBook automatically. That would be a rather efficient way, I think, to carry around part of the Internet's brain trust in an eReader...



http://social.technet.microsoft.com/wiki/contents/articles/776.sql-server-integration-services-ssis.aspx#SSIS_Wiki_Pages



Tuesday, April 9, 2013

Teach Kids To Code C#

Pluralsight just released a training for kids to learn C#.

I'm a fan of things like this:

http://pluralsight.com/training/Courses/TableOfContents/teaching-kids-programming


Another one is ALICE:

http://www.alice.org/index.php

Tuesday, March 26, 2013

Eh?

Ok, so in SQL Server Management Studio 2012, there's automatic function identifying. Nice feature.

SSMS 2008 R2 had something very much the same. So I was running more than one window, containing more than one different version of SSMS. I was building something, and typed this in:


OK, I thought. try_cast isn't available in SQL 2008 R2. I must be in the wrong window. So I switched to SSMS 2012, and pasted in the same statement. Yet, I got the same error.

Eh?