Solved

How best to create a specific file with header and footer incorporating modified select data

Posted on 2014-07-28
3
132 Views
Last Modified: 2014-09-01
Hi

I've got to produce on a daily basis an export file to upload to a payment provider. I'm pulling my data back initially using the following SQL which lists the uprn and price Any ideas for the best way forward? Its well beyond my SQL knowledge but I guess if there is no luck here I could try it all in PHP.

SELECT  b.c_uprn,
        g.green_uprn
	CASE 
		WHEN g.green_uprn IS NOT NULL THEN 10.00
		ELSE 20.00
	END price
FROM    t_blpu b
        LEFT OUTER JOIN t_refuse_garden_2014 g ON b.c_uprn = '0000' + g.green_uprn
ORDER BY 
	g.green_uprn DESC 

Open in new window


Which gives me a file approximately 64372 lines long containing data like this (uprn/price in pence):
40121018	3895
40118160	3895
40117792	3895
40117638	3895
40117634	3895
40117633	3895
40117629	3895

Open in new window


Now, I need to wrap this with a header and footer. The header is quite easy as its preset with just the date (in bold) changing:
H,242,[b]20140727 16:07:00[/b],F,14,7.0

Open in new window


The footer however is trickier as its preset in part but requires the date again then the total number of records, then the amount of all the prices totalled up
T,242,[b]20140727 16:07:00[/b],[b]64372[/b],[b]395828570[/b],0

Open in new window

0
Comment
Question by:NeilT
3 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 40223932
Here's where a UNION query will work for you.  Something like...
<total air code>
SELECT GETDATE() as label, 0 as sort_order_1, 0 as sort_order_2
UNION
SELECT CAST(b.c_uprn as varchar(10) + CAST(g.green_uprn as varchar(10)) + you_get_the_idea as label, 1 as sort_order_1, g.green_uprn as sort_order_2
FROM    t_blpu b
        LEFT OUTER JOIN t_refuse_garden_2014 g ON b.c_uprn = '0000' + g.green_uprn
UNION
SELECT COUNT(see above query) as label, 2 as sort_order_1, 0 as sort_order_2
ORDER BY sort_order_1, sort_order_2

Open in new window

0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 40224533
I wouldn't try to do the data concatenation in SQL Server.  That will lead to maintenance headaches.

Use SQL's bcp utility to separately create the three files, then use a standard DOS/Windows command file to concatenate the files.

Bcp is a command-line utility and is a pain to get working the first time.  But once you've got it going, it's not that bad.  Here's sample code to use bcp to create the header file.

DECLARE @bcp_command varchar(8000)
DECLARE @header_file varchar(500)
DECLARE @body_file varchar(500)
DECLARE @footer_file varchar(500)

SET @header_file = 'C:\header.txt'

SET @bcp_command = 'bcp "SELECT ''H,242,' +
    CONVERT(varchar(8), GETDATE(), 112) + ' ' + CONVERT(varchar(8), GETDATE(), 8) +
    ',F,14,7.0''" queryout "' + @header_file + '" -r\r\n -S server\instance -t, -T -w'
PRINT @bcp_command

EXEC xp_cmdshell @bcp_command
EXEC xp_cmdshell 'type "c:\header.txt"'
EXEC xp_cmdshell 'del "c:\header.txt"'

[Btw, if you do use SQL to concatenate the data, you'd want to use UNION ALL not UNION.]
0
 
LVL 3

Author Closing Comment

by:NeilT
ID: 40297191
Apologies for the delay. Been away on holiday for the last 6 weeks

Great answer, helped instantly

Regards
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now