首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何正确对齐xls以使用SQL Server2008中的存储过程绑定表数据?

如何正确对齐xls以使用SQL Server2008中的存储过程绑定表数据?
EN

Stack Overflow用户
提问于 2012-10-16 13:48:38
回答 1查看 342关注 0票数 0

我像这样编写了存储过程

代码语言:javascript
复制
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

这是很好的创建Excel文件并发送到邮件,但我不能得到正确的格式在一列在一行显示的Excel,如何对齐,请给我任何想法

谢谢你hemanth

EN

回答 1

Stack Overflow用户

发布于 2012-10-17 06:33:16

嗨,看起来你缺少字段分隔符。

代码语言:javascript
复制
queryout C:\WeeklyOnlineSubmissionReport.xls -c -T'
try like this
queryout C:\WeeklyOnlineSubmissionReport.xls  -c -t,' 

-t参数允许您指定字段分隔符。-t后的字符将用于分隔数据字段。如果删除了-t,则将使用制表符作为默认分隔符。请检查以下网站如何使用它

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变量写入文件

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12908305

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档