Solved

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

Posted on 2014-07-28
3
148 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 66

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

628 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