2012-10-16 55 views
0

我寫的存儲過程,這樣如何正確對齊xls以使用SQL Server 2008中的存儲過程綁定表數據?

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='[email protected];' 
     --Select @ToEmail='[email protected]; [email protected]; [email protected]' 
     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 <[email protected]) 
BEGIN 
declare @Ai int 
set @Ai=(select AgentID from #tblAgencyCount where [email protected]) 
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 [email protected] 
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', 
      @[email protected], @subject='Weekly Online Submission Report', 
      @[email protected],@file_attachments='C:\WeeklyOnlineSubmissionReport.xls' 
    End 
End 

--Exec usp_GetTrackAgencyCountReport_Job 

它工作正常創建Excel文件併發送到郵件,但我沒有得到正確的格式在一列一行像那樣顯示Excel,如何對齊,請給我有關

任何想法感謝ü hemanth

回答

0

嗨,似乎你缺少字段分隔符

queryout C:\WeeklyOnlineSubmissionReport.xls -c -T' 
try like this 
queryout C:\WeeklyOnlineSubmissionReport.xls -c -t,' 

-t參數允許您指定字段分隔符。 -t後面的字符將用於分隔數據字段。如果-t被刪除,tab將被用作默認分隔符。 請檢查這些下列網站如何使用它

http://www.mssqltips.com/sqlservertip/1633/simple-way-to-export-sql-server-data-to-text-files/

http://www.simple-talk.com/sql/t-sql-programming/the-tsql-of-text-files/

檢查從TSQL變量本段數據寫入一個文件

相關問題