If statement stored procedure
Hey everyone its peebman2000 again, i'm having trouble with a stored procedure. I have learned and found a way to set up reminder emails using sql 2005 job scheduleing tool. The email works, but I need to write the stored procedure correctly to execute correctly.
Below is my stored procedure i'm trying to write and it's not working. Basically I'll have an email address and a deadline date column:
Table:
Deadline(Id, email, deadline)
So I'll schedule SQL to check the table everyday at noon, and to select the email and deadline date.
If the deadline date = system date then send email to the email address.
And that were i'm struggling at.
Can anyone give me any assistance on writing this type of stored procedure, i'm not too familiar with the if statement in SQL, I've done case statement but not IF statement.
Thanks for any help, I appreciate it.
My stored procedure (not working)
Code:
USE [email]
GO
/****** Object: StoredProcedure [dbo].[proc_happyBirthday] Script Date: 05/20/2008 16:57:39 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[proc_happyBirthday]
(
@birth smalldatetime,
@email varchar(150)
)
AS
BEGIN
SET NOCOUNT ON
select birth
FROM email
if @birth = getdate() then
declare @body1 varchar(100)
set @body1 = 'You belong in a zoo!'---'Server :'+@@servername+ ' My First Database Email '
EXEC msdb.dbo.sp_send_dbmail
@recipients= 'Peebman2000@peebman.com',
--@BCC = @BCCList,
@subject = 'My Mail Test',
@body = @body1,
@body_format = 'text';
end if
END
Error Message:
Msg 156, Level 15, State 1, Procedure proc_happyBirthday, Line 52
Incorrect syntax near the keyword 'END'.
peebman2000, May 21st, 2008 12:10 PM
Bookmarks