SQL服务器为pdf文件生成大量数据

时间:2016-04-07 07:53:32

标签: sql sql-server pdf pdf-generation

我正在创建一个会生成PDF信件的系统,但是我的问题是我的一张桌子上有10000多个数据而且生成这封信需要1个多小时,请问如何改进我的帮助代码减轻其过程?

这是我当前的SQL查询

ALTER PROCEDURE [dbo].[mPDF_gen048] 
@gfromDate VARCHAR(10),
@gtoDate VARCHAR(10) 

AS
BEGIN
SET NOCOUNT ON;

DECLARE @fromDate DATE
DECLARE @toDate DATE
DECLARE @totaldays INT

SET @fromDate = (
        SELECT convert(VARCHAR(10), @gfromDate, 101)
        )
SET @toDate = (
        SELECT convert(VARCHAR(10), @gtoDate, 101)
        )

SET @totaldays = (
        SELECT datediff(day, @fromdate, @toDate) + 2
        )

DECLARE @sql VARCHAR(max) = ''
DECLARE @tablename VARCHAR(20)
DECLARE @counter INT = 1

WHILE @counter < @totaldays
BEGIN
    SET @tablename = ((SELECT REPLACE(UPPER(RIGHT(CONVERT(VARCHAR(11), @fromDate, 106), 8)), ' ', '') AS [Mon-YYYY]))

    SET @tablename = replace(@tablename, ' ', '')

    SET @sql = @sql + 'SELECT 
            ccode,
            refdate,
            refcount,
            accname,
            add1,
            add2,
            zip,
            stat,
            statdate,
            stat2,
            statdate2,
            stat3,
            statdate3,
            address,
            courier,
            pupdate,
            esent,
            recvby,
            rel,
            defbranch FROM gen_048_' + @tablename + ' WHERE refdate = ''' + REPLACE(convert(VARCHAR(10), @fromDate), '-','') + ''''

    IF (@counter < @totaldays - 1)
    begin
        set @sql = @sql + ' union '
    end

        SET @fromDate = dateadd(day, 1, @fromDate)
    SET @counter = @counter + 1
END
END
PRINT @sql
EXEC (@sql)

0 个答案:

没有答案