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
  #21 (permalink)  
Old May 22nd, 2008, 12:27 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 SQL Server Microsoft Windows Microsoft .Net Classic ASP 
Total Awards: 4

Default

I don't see how that could ever work. Basically, you're telling it to send the email to someone who was born today...not 27 years ago on this date. You need to search the database to return records that have a birthdate of 5/22/any year, not 5/22/2008. For this, you can use a Date function: DATEPART (T-SQL)
__________________
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
Sponsored Links
  #22 (permalink)  
Old May 22nd, 2008, 12:46 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

I'm sorry dude I may have confused you during all these post, but I want it to send an email to someone who was born today. I'm testing the email part,because in the app i'm building the user will enter in a date they want a reminder email sent. So i'll schedule sql to run this stored procedure on the column that has the reminder email date. So if the reminder email date = today's date, shoot an email. That way the reminder email is sent on the date the user wants the reminder email.

What I have should work the way I want it.

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

ALTER PROCEDURE [dbo].[sp_proc_happyBirthday2] 
AS 
SET NOCOUNT ON 
declare @birth datetime
declare row cursor for
--select @birth = (select birth from email where birth = birth))
select convert(varchar(10), birth, 120) from email --where birth = birth

open row

fetch row into @birth

while @@fetch_status = 0

begin
	fetch row into @birth
end
close row

Deallocate row

return

--END


 --BEGIN       
IF @birth = convert(varchar(10), getdate(), 120)--getdate()
	
	Begin
        declare @body1 varchar(100)
		
        set @body1 = 'You belong in a zoo!'---'Server :'+@@servername+         EXEC msdb.dbo.sp_send_dbmail 
		--@profile_name = 'david'
        @recipients ='peebman@peebman.com', --
        --@BCC = @BCCList,
        @subject = 'My Mail Test',
        @body = @body1,
        @body_format = 'text';
	end
Quote:
Originally Posted by jmurrayhead View Post
I don't see how that could ever work. Basically, you're telling it to send the email to someone who was born today...not 27 years ago on this date. You need to search the database to return records that have a birthdate of 5/22/any year, not 5/22/2008. For this, you can use a Date function: DATEPART (T-SQL)
Reply With Quote
  #23 (permalink)  
Old May 22nd, 2008, 01:03 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 SQL Server Microsoft Windows Microsoft .Net Classic ASP 
Total Awards: 4

Default

Try this:
Code:
IF CONVERT(varchar(8), @birth, 112) = CONVERT(varchar(8), getdate(), 112)
and wouldn't it make sense to place the email portion within your loop?
Reply With Quote
  #24 (permalink)  
Old May 22nd, 2008, 01:08 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 SQL Server Microsoft Windows Microsoft .Net Classic ASP 
Total Awards: 4

Default

Instead of all of that...why aren't you just using the query to select all records that equal today's date, (i.e. SELECT * FROM email WHERE birth = getdate())? Then manipulate those records...that would make the most sense to me.
Reply With Quote
  #25 (permalink)  
Old May 22nd, 2008, 02:31 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

Dude i'm about to just scratch it, its not working the way I need. The logic and everything should work, but its not. Here what i have, it sends the email now, but again sending it if the date matches getdate or not.

I had a date of 05252008 and it still sent the email, I also had today' date of 05222008 and it still sent the email.

I thought it might be something with the msdb.dbo.sp_send_dbmail function, that it defaults to sending the email no matter what the condition is, but everything I researched shows there no default.

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

ALTER PROCEDURE [dbo].[sp_proc_happyBirthday2] 
AS 
SET NOCOUNT ON 
declare @birth datetime
declare row cursor for
select convert(varchar(8), birth, 112) from email where birth = convert(varchar(8), getdate(), 112)

open row

fetch row into @birth

while @@fetch_status = 0

begin
	fetch row into @birth
IF convert(varchar(8), @birth, 112) = convert(varchar(8), getdate(), 112)-
	Begin
        declare @body1 varchar(100)
		
        set @body1 = 'You belong in a zoo!'   
      EXEC msdb.dbo.sp_send_dbmail 
		
        @recipients ='peebman@peebman.com', --
        --@BCC = @BCCList,
        @subject = 'My Mail Test',
        @body = @body1,
        @body_format = 'html';
	end 
else if @birth <> convert(varchar(8), getdate(), 112)
BEGIN
	EXEC msdb.dbo.sysmail_stop_sp	
	End
end
close row

Deallocate row

return
Quote:
Originally Posted by jmurrayhead View Post
Instead of all of that...why aren't you just using the query to select all records that equal today's date, (i.e. SELECT * FROM email WHERE birth = getdate())? Then manipulate those records...that would make the most sense to me.
Reply With Quote
  #26 (permalink)  
Old May 22nd, 2008, 02:33 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 SQL Server Microsoft Windows Microsoft .Net Classic ASP 
Total Awards: 4

Default

Using Query Analyzer, take this query:

Code:
select convert(varchar(8), birth, 112) from email where birth = convert(varchar(8), getdate(), 112)
and see what records it returns.
Reply With Quote
  #27 (permalink)  
Old May 22nd, 2008, 02:58 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

Well the mother heffer works, I feel like doing some jumping jacks or something.

I entered in today's date into the birth column, again I have 3 records. It shot me an email, 3 emails exactly, I guess because there were 3 records that matched the condition.

Then I changed the date to 05252008 and it shot me NO emails.

Okay so its working now how I want it, and thanks Jmurraryhead.

But I got one more issue. The email addresses in the email column. The current 3 records I have have 3 different email address. How can I include the @email to that if it matches the condition it sends the email to the 3 different email address?

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

ALTER PROCEDURE [dbo].[sp_proc_happyBirthday2] 
AS 
SET NOCOUNT ON 
declare @birth datetime
declare row cursor for
select convert(varchar(8), birth, 112) from email where birth = convert(varchar(8), getdate(), 112)

open row

fetch row into @birth

while @@fetch_status = 0

begin
	fetch row into @birth
IF convert(varchar(8), @birth, 112) = convert(varchar(8), getdate(), 112)
	Begin
        declare @body1 varchar(100)
		
        set @body1 = 'You belong in a zoo!'
        EXEC msdb.dbo.sp_send_dbmail 
		--@profile_name = 'david'
        @recipients = @email, --        --@BCC = @BCCList,
        @subject = 'My Mail Test',
        @body = @body1,
        @body_format = 'html';
	end 
else if convert(varchar(8), @birth, 112) <> convert(varchar(8), getdate(), 112)
BEGIN
	EXEC msdb.dbo.sysmail_stop_sp	
	End
end
close row

Deallocate row

return
Quote:
Originally Posted by jmurrayhead View Post
Using Query Analyzer, take this query:

Code:
select convert(varchar(8), birth, 112) from email where birth = convert(varchar(8), getdate(), 112)
and see what records it returns.
Reply With Quote
  #28 (permalink)  
Old May 22nd, 2008, 03:02 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 SQL Server Microsoft Windows Microsoft .Net Classic ASP 
Total Awards: 4

Default

Glad it's working for you...see Part B here FETCH (T-SQL)
Reply With Quote
  #29 (permalink)  
Old May 22nd, 2008, 03:57 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

Okay jmurrayhead, here's what I got for Part B. but it gives me an error.

Here's the procedure:
Code:
USE [peebman]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[sp_proc_happyBirthday2] 
AS 
SET NOCOUNT ON 
declare @birth datetime, @email varchar(150)

declare row cursor for
select email, convert(varchar(8), birth, 112) from email where birth = convert(varchar(8), getdate(), 112)

open row

fetch next from row into @birth, @email

while @@fetch_status = 0

begin
	fetch next from row into @birth, @email
IF convert(varchar(8), @birth, 112) = convert(varchar(8), getdate(), 112)
	Begin
        declare @body1 varchar(100)
		
        set @body1 = 'You belong in a zoo!'
        EXEC msdb.dbo.sp_send_dbmail 
		
        @recipients = @email,
        --@BCC = @BCCList,
        @subject = 'My Mail Test',
        @body = @body1,
        @body_format = 'html';
	end 
else if convert(varchar(8), @birth, 112) <> convert(varchar(8), getdate(), 112)
BEGIN
	EXEC msdb.dbo.sysmail_stop_sp	
	End
end
close row

Deallocate row

return
And here's the error:
Quote:
conversion failed when converting datetime from character to string
Now I shouldn't get this error because the convert should work.

....Or should I use a Cast for the date, but I thought Cast and Convert are basically the same thing. Any ideas on the error?

Quote:
Originally Posted by jmurrayhead View Post
Glad it's working for you...see Part B here FETCH (T-SQL)
Reply With Quote
  #30 (permalink)  
Old May 23rd, 2008, 08:49 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 SQL Server Microsoft Windows Microsoft .Net Classic ASP 
Total Awards: 4

Default

Not sure what's causing the error all of a sudden. Have you tried it again without the conversion to see if that is actually the issue?
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:37 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