+ Reply to Thread
Page 3 of 4 FirstFirst 1 2 3 4 LastLast
Results 21 to 30 of 33

Thread: If statement stored procedure

  1. #21
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    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
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


  2. #22
    Barn Enthusiast peebman2000 is on a distinguished road peebman2000's Avatar
    Join Date
    Mar 2008
    Posts
    210
    Rep Power
    3

    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)

  3. #23
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    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?
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


  4. #24
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    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.
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


  5. #25
    Barn Enthusiast peebman2000 is on a distinguished road peebman2000's Avatar
    Join Date
    Mar 2008
    Posts
    210
    Rep Power
    3

    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.

  6. #26
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    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.
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


  7. #27
    Barn Enthusiast peebman2000 is on a distinguished road peebman2000's Avatar
    Join Date
    Mar 2008
    Posts
    210
    Rep Power
    3

    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.

  8. #28
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    Glad it's working for you...see Part B here FETCH (T-SQL)
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


  9. #29
    Barn Enthusiast peebman2000 is on a distinguished road peebman2000's Avatar
    Join Date
    Mar 2008
    Posts
    210
    Rep Power
    3

    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:
    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)

  10. #30
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    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?
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


+ Reply to Thread
Page 3 of 4 FirstFirst 1 2 3 4 LastLast

Similar Threads

  1. Dynamic Stored Procedure
    By jmurrayhead in forum Microsoft SQL Server
    Replies: 16
    Last Post: March 26th, 2008, 11:19 AM
  2. Permissions on Tables, Stored Procedures, etc.
    By theChris in forum Microsoft SQL Server
    Replies: 2
    Last Post: March 24th, 2008, 11:49 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO