SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 need help to send mail on insertion of records
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

neellotus
Starting Member

India
14 Posts

Posted - 11/26/2013 :  00:01:12  Show Profile  Reply with Quote
Hi all,
I am trying to sending a mail only if record exist as on current date so i am using sp_send_dbmail within a query as i mentioned below :-

declare @cd varchar(10);
declare @qry varchar(200);
set @cd='LM004'
set @qry='select(select case when sl_code=@cd THEN sl_code else null END as sl_code from test1 where sl_code=@cd and DATEDIFF(DAY,doc_dt,GETDATE())=0) as sl_code'

IF @qry IS NOT NULL
BEGIN
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'SQL Profile',
@recipients = 'testmail@testserver.com',
@subject = 'Inv Created'
END
but it is sending mail even @qry value is null.
Pls tell me where i am doing wrong because i dont want to send mail when record is null or doesn't exist. i will be very thankful to you.

Thanx
neel

Edited by - neellotus on 11/26/2013 00:28:18

khtan
In (Som, Ni, Yak)

Singapore
17439 Posts

Posted - 11/26/2013 :  01:04:43  Show Profile  Reply with Quote
you did not execute any query at all

What you did with set @qry = 'select. .. ' is only store a string of characters in the @qry variable

try changing to
select  @qry = case when sl_code = @cd 
               THEN sl_code 
               else null 
               END as sl_code 
from    test1 
where   sl_code = @cd 
and     DATEDIFF(DAY,doc_dt,GETDATE())=0)



KH
Time is always against us

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 11/26/2013 :  05:36:08  Show Profile  Reply with Quote
As per what you gave @Qry will always be not null as you're storing the query itself in it and not the result of that.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000