|
Hi all, I have a file with a single header row, details(suppose 100 rows) and a trailer row which contains number(or count) of rows in the same file excluding the header and trailer row? How to achieve this? Thanks in advance...
(comments are locked)
|
|
I assume that this question follows the one asked yesterday, which was eventually resolved with the query returning data from 2 tables as one CRLF delimited string. I suppose that this string is then written to the destination flat file. If you would like to continue with this route, but add some extra info to the file (header line on the top and the trailer line on the bottom which will display the number of records) then you can just add one small select for each and union all the results. Here is the sample based on the query in yesterday's comments (I removed majority of the columns from there to keep my answer shorter in size): However, I would like to point out that it is not really necessary to use for xml to concatenate the records like this if SSIS is used to actually create and populate the file. If you have a task pumping data from your 2 tables to the destination flat file and the Row Delimiter property has already been configured to be {CR}{LF} then you should simply return the union all of your selects:
and let SSIS task to take care of adding your records to the file. The only small issue that you need to overcome is the correct sorting of your results. Here is the sample query which is constructed in the way to guarantee that your file will consist of the header on first line, parent and child tables records arranged such that the parent record is followed by all its child records followed by next parent etc, followed by the trailer record displaying the total count of the records in the file (excluding the count of header and trailer lines of course): You can use any of the queries above, but I really believe that the second one is more suitable for SSIS solution while the first one is good enough for a small result set which is easy enough to copy from the query results window by hand (assuming results to text option). Oleg Thx for the answer ... actually i figured out if i use a distinct in SElect statement for table A and no distinct select statement for table B then i will get my desired output ... as if i dont use distinct in table A then i m counting duplicates also where as i m not having any duplicated in the output file from table A.... r u getting me ..... anyways thx alot OLEG.
Mar 18 '11 at 02:37 PM
AbhiD
(comments are locked)
|
|
If what @oleg has asked is correct, that you don't infact have a file with the footer right now you can use a union to add a sum column at the end. Pretty simple to do. I will post an example of a query I have recently created to do the same. SELECT Inst , COUNT(Inst) AS Totals i have header and footer in my output but donno how how to get the row count of the file minus the the header and footer row in the same file.
Mar 18 '11 at 08:39 AM
AbhiD
I think we have enough information on your issue where someone can help out. I however have just finished packing my bags and about to embark on a 5 hour road trip to SQL Saturday #70. I am certain someone will get you the solution to your problem very quickly.
Mar 18 '11 at 08:41 AM
Tim
@abhid, I am so glad that @Oleg was able to get you the results you needed. I hope you continue coming back to ask.sqlservercentral.com to ask questions as well as help others out as well.
Mar 18 '11 at 03:05 PM
Tim
(comments are locked)
|


What exactly is your question? What are you wanting to achieve?
And I want to calculate this row count of this file excluding the header and trailer row for the same file and include this in the same file.
@TRAD r u getting my question?
Sorta getting it. Is this a file you are importing into a table? What version of SQL are you using?
i m creating this file from temp table using ssis ... sql server 2005..i m writing a Sql query on the oledb source to get this file ... i have the correct file with header , details and record but i m not able to include this row count in my trailer row.... how to do this ...