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
 General SQL Server Forums
 New to SQL Server Programming
 Adding condition to script

Author  Topic 

Vack
Aged Yak Warrior

530 Posts

Posted - 2009-05-26 : 12:14:32
Working with the following code.


declare @emailflag char(1)

select @emailflag = emailflag from email
if @emailflag = 'N'
begin

EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'PTC',
@recipients = 'erin.vackert@estagroup.com',
@body = 'Invoices have started posting.',
@subject = 'Invoices have started posting'

end
update email
set emailflag = ' Y'

end


I only want the above code to run if my count is greater than 1.

select count(*) as count
from oehdrhst_sql
where posted_dt > dateadd(mi,-5,getdate())

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2009-05-26 : 12:23:20
[code]
IF (select count(*) as count
from oehdrhst_sql
where posted_dt > dateadd(mi,-5,getdate())
) > 1
Begin
declare @emailflag char(1)

select @emailflag = emailflag from email
if @emailflag = 'N'
begin
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'PTC',
@recipients = 'erin.vackert@estagroup.com',
@body = 'Invoices have started posting.',
@subject = 'Invoices have started posting'

end
update email
set emailflag = ' Y'

end
End
[/code]

Dinakar Nethi
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-05-26 : 12:27:24
wasnt similar question already answered?

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=126236
Go to Top of Page

Vack
Aged Yak Warrior

530 Posts

Posted - 2009-05-26 : 12:48:10
Here is what I have. at the very end where I have

update email
set emailflag = 'Y'

The flag is not updating. The script runs without any errors.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-05-26 : 12:53:46
will email table be always having a single record?
Go to Top of Page

Vack
Aged Yak Warrior

530 Posts

Posted - 2009-05-26 : 12:53:54
Actually I had to change the beginning If statement as well. When I run the script I no longer get an email sent either.

IF (select count(*) as count
from oehdrhst_sql
where month(posted_dt) = month(getdate()) and day(posted_dt) = day(getdate())
and year(posted_dt) = year(getdate()))> 1

BEGIN

declare @emailflag char(1)

select @emailflag = emailflag from email
if @emailflag = 'N'
begin

EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'PTC',
@recipients = 'erin.vackert@estagroup.com',
@body = 'Invoices have started posting.',
@subject = 'Invoices have started posting'

end
begin
update email
set emailflag = 'Y'
end

END
Go to Top of Page

Vack
Aged Yak Warrior

530 Posts

Posted - 2009-05-26 : 12:54:35
Yes.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-05-26 : 12:58:21
you've an extra begin end so its no longer inside if block


IF (select count(*) as count
from oehdrhst_sql
where month(posted_dt) = month(getdate()) and day(posted_dt) = day(getdate())
and year(posted_dt) = year(getdate()))> 1

BEGIN

declare @emailflag char(1)

select @emailflag = emailflag from email
if @emailflag = 'N'
begin

EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'PTC',
@recipients = 'erin.vackert@estagroup.com',
@body = 'Invoices have started posting.',
@subject = 'Invoices have started posting'

end
begin
update email
set emailflag = 'Y'
end

END
Go to Top of Page

Vack
Aged Yak Warrior

530 Posts

Posted - 2009-05-26 : 13:04:09
That didn't seem to do it. emailflag still set to 'N'

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-05-26 : 13:07:22
was mail send atleast?
Go to Top of Page

Vack
Aged Yak Warrior

530 Posts

Posted - 2009-05-26 : 13:10:40
Nope. Just retested and sent a test email and I received an email. So I know that part is setup correctly and working.

If I only run this portion an email is sent as well:

declare @emailflag char(1)

select @emailflag = emailflag from email
if @emailflag = 'N'
begin

EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'PTC',
@recipients = 'erin.vackert@estagroup.com',
@body = 'Invoices have started posting.',
@subject = 'Invoices have started posting'

end
Go to Top of Page

Vack
Aged Yak Warrior

530 Posts

Posted - 2009-05-26 : 13:14:48
I apologize. Stupid mistake on my part. In the beginning I needed "> 0" and not "> 1".

I only had one record in the data I was testing against. Sorry for wasting your time.

It is working now. Thanks
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-05-26 : 13:17:48
cool..glad that you sorted it out
Go to Top of Page
   

- Advertisement -