分享
 
 
 

T-SQL存储过程创建PDF格式文件报表

王朝mssql·作者佚名  2008-05-19
窄屏简体版  字體: |||超大  

This article explains how to create a a stored procedure that will in turn create a simple column based report in PDF without using any external tools or libraries (and their associated licensing costs!).

SQL2PDF makes a PDF report from text inserted in the table psopdf ( nvarchar(80) ). First a table named psopdf should be created.

CREATE TABLE psopdf (code NVARCHAR(80))

After that create the stored procedure SQL2PDF.

SQL2PDF.TXT

And table psopdf has to be filled with your data as shown in examples below.

At the end the stored procedure is called using the file name only (not extension).

EXEC sql2pdf 'fileName'

The result is in your C:\ directory.

EXAMPLE 1:

INSERT psopdf(code) SELECT SPACE(60) + 'COMPANY LTD'

INSERT psopdf(code) SELECT SPACE(60) + 'COMPANY ADDRESS'

INSERT psopdf(code) SELECT SPACE(60) + 'STREET NAME & No'

INSERT psopdf(code) SELECT ' '

INSERT psopdf(code) SELECT SPACE(34) + 'BILL OF SALE'

INSERT psopdf(code) SELECT ' '

INSERT psopdf(code) SELECT 'Product' + SPACE(10) + 'Quantity'

+ SPACE(10) + 'Price' + SPACE(10) + 'Total'

INSERT psopdf(code) SELECT REPLACE(SPACE(56), ' ', '_')

INSERT psopdf(code) SELECT 'Product1' + SPACE(9) + '10.00 '

+ SPACE(10) + '52.30' + SPACE(10) + '5230.0'

INSERT psopdf(code) SELECT 'Product2' + SPACE(9) + '2.00 '

+ SPACE(10) + '10.00' + SPACE(10) + ' 20.0'

INSERT psopdf(code) SELECT REPLACE(SPACE(56), ' ', '_')

INSERT psopdf(code) SELECT SPACE(50) + '5250.0'

After INSERT call the stored procedure with file name demo2.

EXEC sql2pdf 'demo2'

The result is in your C:\ directory.

EXAMPLE 2:

Second example uses a database pubs.

USE pubs

INSERT psopdf(code) SELECT t1.au_lname + ' ' + t1.au_fname + ' ' + t1.phone

+

' ' + t1.address + ' ' + t1.city + ' ' + t1.state + ' ' + t1.zip FROM

authors t1, authors t2

After INSERT call the stored procedure with file name demo1.

EXEC sql2pdf 'demo1'

The result is in your C:\ directory.

sql2pdf.txt

-- DROP PROCEDURE sql2pdf

CREATE PROCEDURE sql2pdf

@filename VARCHAR(100)

AS

CREATE TABLE #pdf (idnumber INT IDENTITY(1,1)

,code NVARCHAR(200))

CREATE TABLE #xref (idnumber INT IDENTITY(1,1)

,code VARCHAR(30))

CREATE TABLE #text (idnumber INT IDENTITY(1,1)

,code VARCHAR(200))

DECLARE @end VARCHAR(7),

@beg

VARCHAR(7),

@a1

VARCHAR(3),

@a2

VARCHAR(3),

@ad

VARCHAR(5),

@cr

VARCHAR(8),

@pr

VARCHAR(9),

@ti

VARCHAR(6),

@xstr

VARCHAR(10),

@page

VARCHAR(8000),

@pdf

VARCHAR(100),

@trenutniRed NVARCHAR(200),

@rows

INT,

@ofset

INT,

@len

INT,

@nopg

INT,

@fs INT,

@ole

INT,

@x INT,

@file

INT,

@object INT

SELECT @pdf = 'C:\' + @filename + '.pdf'

SET @page = ''

SET @nopg = 0

SET @object = 6

SET @end = 'endobj'

SET @beg = ' 0 obj'

SET @a1 = '

SET @a2 = ''

SET @ad = ' 0 R'

SET @cr = CHAR(67) + CHAR(114) + CHAR (101) + CHAR(97) + CHAR(116) + CHAR (111) + CHAR(114)

SET @pr = CHAR(80) + CHAR(114) + CHAR (111) + CHAR(100) + CHAR(117) + CHAR (99 ) + CHAR(101) + CHAR(114)

SET @ti = CHAR(84) + CHAR(105) + CHAR (116) + CHAR(108) + CHAR(101)

SET @xstr = ' 00000 n'

SET @ofset = 396

INSERT INTO #xref(code) VALUES ('xref')

INSERT INTO #xref(code) VALUES ('0 10')

INSERT INTO #xref(code) VALUES ('0000000000 65535 f')

INSERT INTO #xref(code) VALUES ('0000000017' + @xstr)

INSERT INTO #xref(code) VALUES ('0000000790' + @xstr)

INSERT INTO #xref(code) VALUES ('0000000869' + @xstr)

INSERT INTO #xref(code) VALUES ('0000000144' + @xstr)

INSERT INTO #xref(code) VALUES ('0000000247' + @xstr)

INSERT INTO #xref(code) VALUES ('0000000321' + @xstr)

INSERT INTO #xref(code) VALUES ('0000000396' + @xstr)

INSERT INTO #pdf (code) VALUES ('%' + CHAR(80) + CHAR(68) + CHAR (70) + '-1.2')

INSERT INTO #pdf (code) VALUES ('%佑佑')

INSERT INTO #pdf (code) VALUES ('1' + @beg)

INSERT INTO #pdf (code) VALUES (@a1)

INSERT INTO #pdf (code) VALUES ('/' + @cr + ' (Ivica Masar ' + CHAR(80) + CHAR(83) + CHAR (79) + CHAR(80) + CHAR(68) + CHAR (70) + ')')

INSERT INTO #pdf (code) VALUES ('/' + @pr + ' (stored procedure for ms sql

pso@vip.hr)')

INSERT INTO #pdf (code) VALUES ('/' + @ti + ' (SQL2' + CHAR(80) + CHAR(68) + CHAR (70) + ')')

INSERT INTO #pdf (code) VALUES (@a2)

INSERT INTO #pdf (code) VALUES (@end)

INSERT INTO #pdf (code) VALUES ('4' + @beg)

INSERT INTO #pdf (code) VALUES (@a1)

INSERT INTO #pdf (code) VALUES ('/Type /Font')

INSERT INTO #pdf (code) VALUES ('/Subtype /Type1')

INSERT INTO #pdf (code) VALUES ('/Name /F1')

INSERT INTO #pdf (code) VALUES ('/Encoding 5' + @ad)

INSERT INTO #pdf (code) VALUES ('/BaseFont /Courier')

INSERT INTO #pdf (code) VALUES (@a2)

INSERT INTO #pdf (code) VALUES (@end)

INSERT INTO #pdf (code) VALUES ('5' + @beg)

INSERT INTO #pdf (code) VALUES (@a1)

INSERT INTO #pdf (code) VALUES ('/Type /Encoding')

INSERT INTO #pdf (code) VALUES ('/BaseEncoding /WinAnsiEncoding')

INSERT INTO #pdf (code) VALUES (@a2)

INSERT INTO #pdf (code) VALUES (@end)

INSERT INTO #pdf (code) VALUES ('6' + @beg)

INSERT INTO #pdf (code) VALUES (@a1)

INSERT INTO #pdf (code) VALUES ('

/Font ' + @a1 + ' /F1 4' + @ad + ' ' + @a2 + '

/ProcSet [ /' + CHAR(80) + CHAR(68) + CHAR (70) + ' /Text ]')

INSERT INTO #pdf (code) VALUES (@a2)

INSERT INTO #pdf (code) VALUES (@end)

INSERT INTO #text(code) (SELECT code FROM psopdf)

SELECT @x = COUNT(*) FROM #text

SELECT @x = (@x / 60) + 1

WHILE

@nopg

BEGIN

DECLARE SysKursor

INSENSITIVE SCROLL CURSOR

FOR SELECT SUBSTRING((code + SPACE(81)), 1, 80) FROM #text WHERE idnumber BETWEEN ((@nopg * 60) + 1) AND ((@nopg + 1) * 60 )

FOR READ ONLY

OPEN SysKursor

FETCH NEXT FROM SysKursor INTO @trenutniRed

SELECT @object = @object + 1

SELECT @page = @page +

' ' + CAST(@object AS VARCHAR) + @ad

SELECT @len = LEN(@object) + LEN(@object + 1)

INSERT INTO #pdf (code) VALUES (CAST(@object AS VARCHAR)

+ @beg)

INSERT INTO #pdf (code) VALUES (@a1)

INSERT INTO #pdf (code) VALUES ('/Type /Page')

INSERT INTO #pdf (code) VALUES ('/Parent 3' + @ad)

INSERT INTO #pdf (code) VALUES ('/Resources 6' + @ad)

SELECT @object = @object + 1

INSERT INTO #pdf (code) VALUES ('/Contents ' + CAST(@object AS VARCHAR) + @ad)

INSERT INTO #pdf (code) VALUES (@a2)

INSERT INTO #pdf (code) VALUES (@end)

SELECT @ofset = @len + 86 + @ofset

INSERT INTO #xref(code) (SELECT SUBSTRING('0000000000' + CAST(@ofset AS VARCHAR),

LEN('0000000000' + CAST(@ofset AS VARCHAR)) - 9,

LEN('0000000000' + CAST(@ofset AS VARCHAR))) + @xstr)

INSERT INTO #pdf (code) VALUES (CAST(@object AS VARCHAR)

+ @beg)

INSERT INTO #pdf (code) VALUES (@a1)

SELECT @object = @object + 1

INSERT INTO #pdf (code) VALUES ('/Length ' + CAST(@object AS VARCHAR) + @ad)

INSERT INTO #pdf (code) VALUES (@a2)

INSERT INTO #pdf (code) VALUES ('stream')

INSERT INTO #pdf (code) VALUES ('BT')

INSERT INTO #pdf (code) VALUES ('/F1 10

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有