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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 THrough job sp does not able to send mail

Author  Topic 

imranabdulaziz
Yak Posting Veteran

83 Posts

Posted - 2008-12-26 : 01:16:12
Dear all,
i am using sql server 2005
I 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 help

spmtd is as
ALTER procedure [dbo].[spMTD](
@div varchar(50)
)
as
DECLARE @ECODE VARCHAR(50)
declare @stmdate datetime
declare @endmdate datetime
declare @stydate datetime
declare @endydate datetime
declare @mtotalcall int
declare @noofworkingday int
declare @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 = 1
select @maxemp=max(rowno) from @tran
while(@cnt <= @maxemp)
begin
declare @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 , @ytd
set @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))
as
declare @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.vwempsub
IF(@YTD < 10)
SET @MSG = 'which is not as per the working norms'
else
set @msg = ''
select @email = er_emp_email from bbraun_emis.dbo.vwempsub where empcode = @empcode
select @bossemail = er_emp_email from bbraun_emis.dbo.vwempsub where empcode = @bosscode
set @cmtd = convert(varchar(50) , @mtd)
set @cytd = convert(varchar(50) , @ytd)
set @bbody = ‘test’
if(@email is not null)
begin
exec msdb.dbo.sp_send_dbmail @profile_name = 'IMRAN' , @recipients=@email , @copy_recipients = @bossemail , @body = @bbody , @body_format = 'HTML' ,@subject = 'subject' ,
end


visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-27 : 12:46:40
http://social.msdn.microsoft.com/forums/en-US/sqltools/thread/32a2c09a-156f-43b2-9076-b11ba9ccdafb/
Go to Top of Page
   

- Advertisement -