I wrote the stored procedure like this
CREATE Procedure [dbo].[usp_GetTrackAgencyCountReport_Job]
AS
Declare @BodyMessage Varchar(1000),
@ToEmail Varchar(254),
@sql Varchar(max),
@Count int,
@intFlag INT,
@Agent varchar(200),
@From datetime,
@To datetime,
@DbName varchar(100)
SET @BodyMessage='Please find attached list of Agency names, for how many accounts rated, how many accounts submitted and how many accounts approved to QMS. '
Select @ToEmail='hemanth@zeninfotech.com;'
--Select @ToEmail='cpeterson@chris-leef.com; gpeterson@chris-leef.com; info@chris-leef.com'
SET @To= (SELECT DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0))
SET @From=(SELECT DATEADD(day, DATEDIFF(day, 0, GETDATE()), -7))
SET @Agent=''
SET @DbName='QMSV3Dev'
Begin
--crete temp table to get the details
CREATE TABLE #tblAgencyCount(AID INT IDENTITY(1,1) NOT NULL,SaveDataCount varchar(50),AgentID varchar(50),GetRateCount varchar(50),ConvertedQuoteCount varchar(50))
INSERT INTO #tblAgencyCount(SaveDataCount,AgentID) select COUNT(*),AQ.AgentID from Agent_Quote AQ where
AQ.CreatedOn between @From and @To
and AQ.AgentID like @Agent +'%' and AQ.StatusCode!='C' group by AQ.AgentID
SET @intFlag = 1
set @Count=(select COUNT(*) from #tblAgencyCount)
WHILE (@intFlag <=@Count)
BEGIN
declare @Ai int
set @Ai=(select AgentID from #tblAgencyCount where AID=@intFlag)
Update #tblAgencyCount set GetRateCount=(select COUNT(*) from Agent_Quote AQ where
AQ.CreatedOn between @From and @To
and AQ.AgentID like @Ai and AQ.StatusCode!='C' and AQ.LowPrice!='' and AQ.HighPrice!=''),
ConvertedQuoteCount=(select COUNT(*) from Agent_Quote AQ where
AQ.CreatedOn between @From and @To
and AQ.AgentID like @Ai and AQ.StatusCode!='C' and AQ.ApproveStatus='Approved')
where AID=@intFlag
SET @intFlag = @intFlag + 1
END
--end
Drop Table Temp_AgencyCount
Create Table Temp_AgencyCount(AgencyName Varchar(250),SubmittedCount Varchar(50),GetRateCount varchar(50),ApprovedQuoteCount Varchar(50))
SET @sql='select A.Name,AC.SaveDataCount as SubmittedCount,AC.GetRateCount,AC.ConvertedQuoteCount as ApprovedQuoteCount from #tblAgencyCount AC Left Join ' + @DbName + '..Agent A On AC.AgentID = A.ID order by A.Name'
Insert into Temp_AgencyCount Values('Agency Name','Submitted Count','GetRate Count','Approved QuoteCount')
Insert into Temp_AgencyCount Values('------------------------------------','-----------------','---------------','-------------------')
Insert into Temp_AgencyCount Exec(@sql)
exec master..xp_cmdshell 'bcp "SELECT AgencyName+Space(40-Len(AgencyName))+ ''- ''+ SubmittedCount+Space(20-Len(SubmittedCount))+''- ''+GetRateCount+Space(20-Len(GetRateCount))+''- ''+ApprovedQuoteCount FROM AgentPortal_Live_21Sept2012..Temp_AgencyCoun
t" queryout C:\WeeklyOnlineSubmissionReport.xls -c -T'
Select @Count=COUNT(*) from Temp_AgencyCount
If(@Count!=0)
Begin
EXEC msdb.dbo.sp_send_dbmail @profile_name='Profile',
@recipients=@ToEmail, @subject='Weekly Online Submission Report',
@body=@BodyMessage,@file_attachments='C:\WeeklyOnlineSubmissionReport.xls'
End
End
--Exec usp_GetTrackAgencyCountReport_Job
It is working fine to create the Excel file and send to mail but I don't get correct format in one column in one line like that to display the Excel, how to align, please give me any idea about that
Thank u hemanth