DeveloperBarn Forums

Go Back   DeveloperBarn Forums > Databases > SQL Development

Discuss "If statement stored procedure" in the SQL Development forum.

SQL Development - Structured Query Language (SQL) is the talk of databases. Discuss topics such as joins, triggers and other advanced SQL topics.


Reply « Previous Thread | Next Thread »
 
LinkBack (1) Thread Tools Display Modes
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old May 21st, 2008, 12:10 PM
Contributing Member

 
Join Date: Mar 2008
Posts: 106
Thanks: 8
Thanked 0 Times in 0 Posts
Rep Power: 1
peebman2000 is an unknown quantity at this point
Question 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:
Quote:
Msg 156, Level 15, State 1, Procedure proc_happyBirthday, Line 52
Incorrect syntax near the keyword 'END'.
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old May 21st, 2008, 01:11 PM
jmurrayhead's Avatar
Your Lord & Master

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 534
Thanks: 14
Thanked 39 Times in 38 Posts
Blog Entries: 2
Rep Power: 1
jmurrayhead will become famous soon enough

Awards Showcase
Microsoft Windows Microsoft .Net Microsoft SQL Server Classic ASP 
Total Awards: 4

Default

Try this:

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 
    SET NOCOUNT ON 
 
BEGIN   
    select birth
            FROM email 
END
        
IF @birth = getdate()
    BEGIN
        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
__________________
jmurrayhead
Did I help you out? Make me popular by clicking the icon!

If you found a post helpful, please click the button in the lower right-hand corner of the post.

Powered by ASP.Net
Reply With Quote
  #3 (permalink)  
Old May 21st, 2008, 02:29 PM
Contributing Member

 
Join Date: Mar 2008
Posts: 106
Thanks: 8
Thanked 0 Times in 0 Posts
Rep Power: 1
peebman2000 is an unknown quantity at this point
Question reply

Hey jmurrayhead, thanks; but I tried your suggestion and I make the date in the birth column as 05/23/2008 12:00:00 AM and the procedure still sends the email.

Todays the 21 st so its suppose to send the email if the birth column = today's date, but it doesn't work it still sends the email no matter what.

Any other thoughts? I wander if its has to do with the msdb.dbo.sp_send_dbmail part of the email.

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 datetime,
	@email varchar(150)
)
AS 
SET NOCOUNT ON 
 
BEGIN   
    select birth
            FROM email 
END
        
IF @birth = getdate()
    BEGIN
        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@peeman.com',
        --@BCC = @BCCList,
        @subject = 'My Mail Test',
        @body = @body1,
        @body_format = 'text';
    END
Quote:
Originally Posted by jmurrayhead View Post
Try this:

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 
    SET NOCOUNT ON 
 
BEGIN   
    select birth
            FROM email 
END
        
IF @birth = getdate()
    BEGIN
        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
Reply With Quote
  #4 (permalink)  
Old May 21st, 2008, 02:50 PM
Contributing Member

 
Join Date: Mar 2008
Posts: 106
Thanks: 8
Thanked 0 Times in 0 Posts
Rep Power: 1
peebman2000 is an unknown quantity at this point
Question another reply

Hey Jmurrayhead how about something like this, like if the date does not match stop procedure, else send email. I'm trying to get it to send the email if the date matches only instead of it sending the email if the date matches or not.

Code:
IF @birth <> getdate()
	stop
else
    BEGIN
        declare @body1 varchar(100)
		
        set @body1 = 'You belong in a zoo!'---'Server :'+@@servername+ ' My First Database Email '
        EXEC msdb.dbo.sp_send_dbmail 
		--@profile_name = 'david'
        @recipients = @email, --Peebman2000@peebman.com,
        --@BCC = @BCCList,
        @subject = 'My Mail Test',
        @body = @body1,
        @body_format = 'text';

	  
    END
Quote:
Originally Posted by jmurrayhead View Post
Try this:

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 
    SET NOCOUNT ON 
 
BEGIN   
    select birth
            FROM email 
END
        
IF @birth = getdate()
    BEGIN
        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
Reply With Quote
  #5 (permalink)  
Old May 21st, 2008, 02:57 PM
jmurrayhead's Avatar
Your Lord & Master

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 534
Thanks: 14
Thanked 39 Times in 38 Posts
Blog Entries: 2
Rep Power: 1
jmurrayhead will become famous soon enough

Awards Showcase
Microsoft Windows Microsoft .Net Microsoft SQL Server Classic ASP 
Total Awards: 4

Default

I'm not very familiar some of these features, but maybe something like this then:

Code:
IF @birth <> getdate()
    BEGIN    
        Return 0 
    END
ELSE
    BEGIN
        declare @body1 varchar(100)
 
        set @body1 = 'You belong in a zoo!'---'Server :'+@@servername+ ' My First Database Email '
        EXEC msdb.dbo.sp_send_dbmail 
        --@profile_name = 'david'
        @recipients = @email, --Peebman2000@peebman.com,
        --@BCC = @BCCList,
        @subject = 'My Mail Test',
        @body = @body1,
        @body_format = 'text';
 
 
    END
Reply With Quote
  #6 (permalink)  
Old May 21st, 2008, 03:26 PM
Contributing Member

 
Join Date: Mar 2008
Posts: 106
Thanks: 8
Thanked 0 Times in 0 Posts
Rep Power: 1
peebman2000 is an unknown quantity at this point
Question reply

This has to work, i'm researching online, do you know how to stop a stored procedure within a stored procedure?

Quote:
Originally Posted by jmurrayhead View Post
I'm not very familiar some of these features, but maybe something like this then:

Code:
IF @birth <> getdate()
    BEGIN    
        Return 0 
    END
ELSE
    BEGIN
        declare @body1 varchar(100)
 
        set @body1 = 'You belong in a zoo!'---'Server :'+@@servername+ ' My First Database Email '
        EXEC msdb.dbo.sp_send_dbmail 
        --@profile_name = 'david'
        @recipients = @email, --Peebman2000@peebman.com,
        --@BCC = @BCCList,
        @subject = 'My Mail Test',
        @body = @body1,
        @body_format = 'text';
 
 
    END
Reply With Quote
  #7 (permalink)  
Old May 22nd, 2008, 08:39 AM
jmurrayhead's Avatar
Your Lord & Master

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 534
Thanks: 14
Thanked 39 Times in 38 Posts
Blog Entries: 2
Rep Power: 1
jmurrayhead will become famous soon enough

Awards Showcase
Microsoft Windows Microsoft .Net Microsoft SQL Server Classic ASP 
Total Awards: 4

Default

Not sure what you mean, but the IF statement should prevent the procedure from even being called.
Reply With Quote
  #8 (permalink)  
Old May 22nd, 2008, 08:55 AM
Contributing Member

 
Join Date: Mar 2008
Posts: 106
Thanks: 8
Thanked 0 Times in 0 Posts
Rep Power: 1
peebman2000 is an unknown quantity at this point
Question reply

Hey jmurrayhead thanks, I found out the reason why it kept sending the emails with out checking the birth(date column). I had the store procedure excecuting off the wrong database, I have another database with a practice table for sending emails.

But now I have it on the right database and I'm using this stored procedure:
Code:
USE [peebman]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[sp_proc_happyBirthday] 
(
	@birth datetime,
	@email varchar(150)
)
AS 
SET NOCOUNT ON 
 
BEGIN   
    select birth, email
            FROM email 
END

 --BEGIN       
IF @birth = getdate()--getdate()
	Begin
        declare @body1 varchar(100)
		
        set @body1 = 'You belong in a zoo!'---'Server :'+@@servername+         EXEC msdb.dbo.sp_send_dbmail 
	  @recipients = @email,        --@BCC = @BCCList,
        @subject = 'My Mail Test',
        @body = @body1,
        @body_format = 'text';
	--end if
	  
  END
And I get this error when I schedule the job to run this stored procedure:

Quote:
Procedure or function sp_proc_happybirthday expectes parameter @birth, which was not supplied
the step failed

Should I like set @birth = birth, I don't know any ideas?


Quote:
Originally Posted by jmurrayhead View Post
Not sure what you mean, but the IF statement should prevent the procedure from even being called.
Reply With Quote
  #9 (permalink)  
Old May 22nd, 2008, 08:59 AM
jmurrayhead's Avatar
Your Lord & Master

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 534
Thanks: 14
Thanked 39 Times in 38 Posts
Blog Entries: 2
Rep Power: 1
jmurrayhead will become famous soon enough

Awards Showcase
Microsoft Windows Microsoft .Net Microsoft SQL Server Classic ASP 
Total Awards: 4

Default

To set variables, you do this:
Code:
SET @variable = 'whatever'
However, it appears that the parameter @birth isn't being supplied to the stored procedure.
Reply With Quote
  #10 (permalink)  
Old May 22nd, 2008, 09:09 AM
Contributing Member

 
Join Date: Mar 2008
Posts: 106
Thanks: 8
Thanked 0 Times in 0 Posts
Rep Power: 1
peebman2000 is an unknown quantity at this point
Question reply

Thanks, I don't know I don't understand. If I use the simple select statement that used in the stored procedure:

Code:
select birth, email
            FROM email
It brings back this, and I have three records in the table.
Column(which is the date column)
birth(05/22/2008 12:00:00 AM, 05/23/2008 12:00:00 AM, 05/23/2008 12:00:00 AM)

email(peebman2000@peeb.com, peeb@peeb.com, peebman@peebman.com)

So the dates are in the birth column, I don't understand why the date from the birth column is not supplied to the stored procedure.

Code:
USE [peebman]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[sp_proc_happyBirthday] 
(
	@birth datetime,	@email varchar(150)
)
AS 
SET NOCOUNT ON 
 
BEGIN   
    select birth, email
            FROM email 
END

IF @birth = getdate()--getdate()
	
	Begin
        declare @body1 varchar(100)
		
        set @body1 = 'You belong in a zoo!'---'Server :'+@@servername+         EXEC msdb.dbo.sp_send_dbmail 
		        @recipients = @email,        --@BCC = @BCCList,
        @subject = 'My Mail Test',
        @body = @body1,
        @body_format = 'text';
	--end if
	  
  END



Quote:
Originally Posted by jmurrayhead View Post
To set variables, you do this:
Code:
SET @variable = 'whatever'
However, it appears that the parameter @birth isn't being supplied to the stored procedure.
Reply With Quote
Reply

  DeveloperBarn Forums > Databases > SQL Development

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic Stored Procedure jmurrayhead Microsoft SQL Server 16 March 26th, 2008 11:19 AM
Permissions on Tables, Stored Procedures, etc. theChris Microsoft SQL Server 2 March 24th, 2008 11:49 AM

LinkBacks (?)
LinkBack to this Thread: http://www.developerbarn.com/sql-development/206-if-statement-stored-procedure.html
Posted By For Type Date
In need of advise - ASP Free This thread Refback May 29th, 2008 07:40 AM


Sponsored Links

ASP.NET Resource Index
a directory of ASP.NET tutorials, applications, scripts, assemblies and articles for the novice to professional developer.

Free Web Directory
Including Chats and Forums Resources, Offer automatic, instant and free directory submissions.
URLZ Web Directory
URLZ Web Directory

Free Web Directory - Add Your Link
The Little Web Directory
Free Web Directory
Pegasus free web directory is a free directory organised by categories.

Web Directory & SEO Services
dirroot web directory


All times are GMT -4. The time now is 10:54 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0
Copyright © 2008 DeveloperBarn.com

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46