Dear all,i am using sql server 2005I am using Stored Procedure to send mail based on certain condition. When I excute sp everything works fine but I schedules the same sp in job, job runs successfully but its does not send mail. I have used nested sp to exec the mail that is main sp named spmtd call spavgmail to send mail indiviaully. In job I schedule this statement to run exec spMTD 'divn'below is code for both sp Please helpspmtd is asALTER procedure [dbo].[spMTD](@div varchar(50))asDECLARE @ECODE VARCHAR(50) declare @stmdate datetimedeclare @endmdate datetimedeclare @stydate datetimedeclare @endydate datetimedeclare @mtotalcall intdeclare @noofworkingday intdeclare @callavg decimal(18,2)DECLARE @EMPLIST TABLE(ECODE VARCHAR(50) ) DECLARE @Tran TABLE(rowno int,ECODE VARCHAR(50) , mtotalcall decimal(18 , 2) , mbusday decimal(18 , 2) , mtd decimal(18 , 2) , ytotalcall decimal(18 , 2) , ybusday decimal(18 , 2) , ytd decimal(18 , 2) ) set @stmdate = DATEADD(mm, DATEDIFF(mm,0,getdate()), 0)set @endmdate = getdate()set @stydate = DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)set @endydate = getdate()INSERT INTO @EMPLIST(ECODE ) select * FROM bbraun_emis.dbo.fngetemp_div(@div)insert into @tran( rowno,ecode , mtotalcall , mbusday , mtd , ytotalcall , ybusday , ytd ) SELECT Row_Number() over (order by ecode) , ECODE AS EMPID ,dbo.[gettotalcall](@stmdate , @endmdate , ECODE) as mtotalcall , dbo.[getbusday](@stmdate , @endmdate , ECODE) as mworkingday , convert ( decimal(18,2) , (dbo.[gettotalcall](@stmdate , @endmdate , ECODE) / dbo.[getbusday](@stmdate , @endmdate , ECODE)) ) as mtd ,dbo.[gettotalcall](dbo.fngetfirstreporting(ecode) , @endydate , ECODE) as ytotalcall , dbo.[getbusday](dbo.fngetfirstreporting(ecode) , @endydate , ECODE) as yworkingday , convert ( decimal(18,2) , (dbo.[gettotalcall](dbo.fngetfirstreporting(ecode) , @endydate , ECODE) / dbo.[getbusday](dbo.fngetfirstreporting(ecode) , @endydate , ECODE)) ) as ytd FROM @EMPLIST declare @maxemp int declare @cnt int set @cnt = 1select @maxemp=max(rowno) from @tran while(@cnt <= @maxemp)begindeclare @emp varchar(50)declare @bosscode varchar(50)declare @mtd decimal(18,2)declare @ytd decimal(18,2)select @emp=ecode , @bosscode = dbo.getbosscode(ecode) , @mtd=mtd , @ytd= ytd from @tran where rowno = @cnt and mtd < 10 if(@@rowcount <> 0)exec SPAvgMail @emp , @bosscode , @mtd , @ytdset @cnt = @cnt + 1 end
And SP spavgmail is ALTER PROCEDURE [dbo].[SpCallAvgMail](@empcode varchar(50) , @bosscode varchar(50) , @mtd decimal(18 , 2) , @ytd decimal(18,2))asdeclare @email nvarchar(100)declare @bossemail nvarchar(100)declare @cmtd varchar(50)declare @cytd varchar(50)DECLARE @MSG VARCHAR(300)declare @bbody varchar(max)--select * from bbraun_emis.dbo.vwempsubIF(@YTD < 10)SET @MSG = 'which is not as per the working norms'elseset @msg = ''select @email = er_emp_email from bbraun_emis.dbo.vwempsub where empcode = @empcodeselect @bossemail = er_emp_email from bbraun_emis.dbo.vwempsub where empcode = @bosscodeset @cmtd = convert(varchar(50) , @mtd)set @cytd = convert(varchar(50) , @ytd)set @bbody = ‘test’if(@email is not null)beginexec msdb.dbo.sp_send_dbmail @profile_name = 'IMRAN' , @recipients=@email , @copy_recipients = @bossemail , @body = @bbody , @body_format = 'HTML' ,@subject = 'subject' ,end