Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Email with Case statement

Author  Topic 

Mallen
Starting Member

27 Posts

Posted - 2011-02-22 : 14:52:31
I am trying to put an email into a case statement.

The procedure should check to see if another procedure completed successfully for the night. If so it runs the next procedure. If NOT I would like it to send me an email.

It is returning the error 'Incorrect syntax near the keyword 'EXEC' and Incorrect syntax near the keyword 'ELSE'. Everything works perfect if I take out the email EXEC statements and just throw in text so there is something wrong with how I am writing the email part. How should I do this correctly?

This is what I have:

-- Capture date of most recently loaded results
DECLARE @Date datetime
SET @Date = (SELECT TOP 1 DateEntered FROM ReturnFile ORDER BY DateEntered DESC)
-- Capture process status of moce recently loaded results
DECLARE @PStatus bit
SET @PStatus = (SELECT TOP 1 IsProcessed FROM ReturnFile ORDER BY DateEntered DESC)

-- Check to ensure results have been processed.
SELECT
CASE
WHEN ((CONVERT(VARCHAR(10),@Date,111)) = (CONVERT(VARCHAR(10),GETDATE(),111))) AND @PStatus=1
THEN 'Run written procedure here'
WHEN ((CONVERT(VARCHAR(10),@Date,111)) = (CONVERT(VARCHAR(10),GETDATE(),111))) AND @PStatus=0
THEN
EXEC msdb.dbo.sp_send_dbmail @recipients='procedures@xxx.com',
@subject = 'TS Results Test',
@body = 'TS Results are not finished processing';
ELSE
EXEC msdb.dbo.sp_send_dbmail @recipients='procedures@xxx.com',
@subject = 'TS Results Test',
@body = 'TS Results did not load';

END

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-02-22 : 15:20:42
You can not use SELECT CASE like that. You will instead need to use IF logic.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -