Solved

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

Posted on 2014-07-28
3
129 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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.​
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

747 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

14 Experts available now in Live!

Get 1:1 Help Now