T-SQL stored procedures create PDF files (reports)

Source: Internet
Author: User
Document directory
  • EXAMPLE 1:
  • EXAMPLE 2:

The post is referenced only by FSO. You can write it in PDF format, just like the markup language:

Creating a PDF from a Stored Procedure

Http://www.sqlservercentral.com/columnists/mivica/creatingapdffromastoredprocedure.asp

 

Post it if you do not want to register:

Creating a PDF from a Stored Procedure

Regular Columnist: M Ivica
Posted: 08/26/2003
More Articles From This Columnist
14089 Reads

Add Article to Your Virtual Briefcase(What is this ?)

Article RatingTotal number of votes [183]

This Content already sored:

Is SQL the Center of your Universe?
SQLCentric is a comprehensive web-based network database monitoring and alert system.-brought to you by Pearl Knowledge Solutions, Inc.
Http://www.pearlknows.com

This article explains how to create 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 shocould 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 pubsINSERT 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.

 

Sql22.16.txt

-- Drop procedure sql2pdfCREATE PROCEDURE sql2pdf @ filename VARCHAR (100) as create table # pdf (idnumber int identity (200), code NVARCHAR )) create table # xref (idnumber int identity (200), code VARCHAR (30) create table # text (idnumber int identity (), code VARCHAR )) 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 + 'region' 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 = '2014 n' SET @ ofset = 00000 insert into # xref (code) VALUES ('xref ') insert into # xref (code) VALUES ('0 10 ') insert into # xref (code) VALUES ('1970 0000000000 F') insert into # xref (code) VALUES ('20140901' + @ xstr) insert into # xref (code) VALUES ('000000' + @ xstr) insert into # xref (code) VALUES ('000000' + @ xstr) insert into # xref (code) VALUES ('000000' + @ xstr) insert into # xref (code) VALUES ('000000' + @ xstr) insert into # xref (code) VALUES ('000000' + @ xstr) insert into # xref (code) VALUES ('000000' + @ xstr) insert into # pdf (code) VALUES ('%' + CHAR (80) + CHAR (68) + CHAR (70) + '-1.2') insert into # pdf (code) VALUES ('% you ') 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 <@ x 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 ('100' + CAST (@ ofset as varchar), LEN ('100' + CAST (@ ofset as varchar)-9, LEN ('200' + 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 tf ') insert into # pdf (code) VALUES ('1 0 0 1 50 802 Tm ') insert into # pdf (code) VALUES ('12 TL ') WHILE @ Fetch_Status = 0 begin insert into # pdf (code) VALUES ('t * ('+ @ trenutniRed +') Tj ') fetch next from SysKursor INTO @ trenutniRed end insert into # pdf (code) VALUES ('et') insert into # pdf (code) VALUES ('endstream ') insert into # pdf (code) VALUES (@ end) SELECT @ rows = (select count (*) FROM # text WHERE idnumber BETWEEN (@ nopg * 60) + 1) AND (@ nopg + 1) * 60) * 90 + 45 SELECT @ nopg = @ nopg + 1 SELECT @ len = LEN (@ object) + LEN (@ object-1) SELECT @ ofset = @ len + 57 + @ ofset + @ rows insert into # xref (code) (select substring ('100' + CAST (@ ofset as varchar), LEN ('100' + CAST (@ ofset as varchar)-9, LEN ('200' + CAST (@ ofset as varchar) + @ xstr) insert into # pdf (code) VALUES (CAST (@ object as varchar) + @ beg) insert into # pdf (code) VALUES (@ rows) insert into # pdf (code) VALUES (@ end) SELECT @ len = LEN (@ object) + LEN (@ rows) SELECT @ ofset = @ len + 18 + @ ofset insert into # xref (code) (select substring ('20140901' + CAST (@ ofset as varchar ), LEN ('200' + CAST (@ ofset as varchar)-9, LEN ('200' + CAST (@ ofset as varchar) + @ xstr) CLOSE SysKursor DEALLOCATE SysKursor end insert into # pdf (code) VALUES ('2' + @ beg) insert into # pdf (code) VALUES (@ a1) insert into # pdf (code) VALUES ('/Type/Catalog') insert into # pdf (code) VALUES ('/Pages 3' + @ ad) insert into # pdf (code) VALUES ('/PageLayout/OneColumn') insert into # pdf (code) VALUES (@ a2) insert into # pdf (code) VALUES (@ end) UPDATE # xref SET code = (SELECT code FROM # xref WHERE idnumber = (select max (idnumber) FROM # xref )) WHERE idnumber = 5 delete from # xref WHERE idnumber = (select max (idnumber) FROM # xref) insert into # pdf (code) VALUES ('3' + @ beg) insert into # pdf (code) VALUES (@ a1) insert into # pdf (code) VALUES ('/Type/page') insert into # pdf (code) VALUES ('/count' + CAST (@ nopg as varchar) insert into # pdf (code) VALUES ('/MediaBox [0 595 842] ') insert into # pdf (code) VALUES ('/Kids [' + @ page + ']') insert into # pdf (code) VALUES (@ a2) insert into # pdf (code) VALUES (@ end) SELECT @ ofset = @ ofset + 79 UPDATE # xref SET code = (select substring ('000000' + CAST (@ ofset as varchar ), LEN ('200' + CAST (@ ofset as varchar)-9, LEN ('200' + CAST (@ ofset as varchar) + @ xstr) WHERE idnumber = 6 insert into # xref (code) VALUES ('trailer') insert into # xref (code) VALUES (@ a1) SELECT @ object = @ object + 1 UPDATE # xref SET code = '0' + CAST (@ object as varchar) WHERE idnumber = 2 insert into # xref (code) VALUES ('/size' + CAST (@ object as varchar) insert into # xref (code) VALUES ('/Root 2' + @ ad) insert into # xref (code) VALUES ('/Info 1' + @ ad) insert into # xref (code) VALUES (@ a2) insert into # xref (code) VALUES ('startxref ') SELECT @ len = LEN (@ nopg) + LEN (@ page) SELECT @ ofset = @ len + 86 + @ ofset insert into # xref (code) VALUES (@ ofset) insert into # xref (code) VALUES ('%' + CHAR (69) + CHAR (79) + CHAR (70) insert into # pdf (code) (SELECT code FROM # xref) -- SELECT code FROM # pdf SELECT @ trenutniRed = 'del' + @ pdf EXECUTE @ ole = sp_OACreate 'scripting. fileSystemObject ', @ fs out exec master .. xp_export shell @ trenutniRed, NO_OUTPUT EXECUTE @ ole = sp_OAMethod @ fs, 'opentextfile', @ file OUT, @ pdf, 8, 1 DECLARE SysKursor insensitive scroll cursor for select code FROM # pdf order by idnumber for read only open SysKursor fetch next from SysKursor INTO @ trenutniRed WHILE @ Fetch_Status = 0 begin execute @ ole = lead @ file, 'writeline ', Null, @ brief fetch next from SysKursor INTO @ trenutniRed end close SysKursor DEALLOCATE SysKursor delete from psopdf EXECUTE @ ole = sp_OADestroy @ file EXECUTE @ ole = sp_OADestroy @ fs

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.