Solved

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

Posted on 2014-07-28
3
137 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:Neil Thompson
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:Scott Pletcher
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:Neil Thompson
ID: 40297191
Apologies for the delay. Been away on holiday for the last 6 weeks

Great answer, helped instantly

Regards
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

829 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