Author |
Topic |
spidey2005
Starting Member
3 Posts |
Posted - 2006-10-30 : 04:41:48
|
Hi,I have a situation where I have to generate a output fileconsisting of header record,detail record and trailer record.I have created a DTS and it works fine.There is an application which releases data to the header and trailer tables and I use dts to generate the output file.The output file should be of the form(a sample)H|Cust_ID|Datecreated|DatereleasedD|value1|value2|Cust_ID|DatereleasedD|value1|value2|Cust_ID|DatereleasedD|value1|value2|Cust_ID|DatereleasedD|value1|value2|Cust_ID|DatereleasedD|value1|value2|Cust_ID|DatereleasedD|value1|value2|Cust_ID|DatereleasedT|8The problem is each file should have a distinct cust_id.If the application releases data related to a different cust_id thenI might get a combination of 2 or more headers with the related detail records which I do not want.How I do solve this problem?Regards,Spidey |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-30 : 05:18:49
|
How does your query like like today for exporting the data?Peter LarssonHelsingborg, Sweden |
|
|
spidey2005
Starting Member
3 Posts |
Posted - 2006-10-30 : 06:04:51
|
quote: Originally posted by Peso How does your query like like today for exporting the data?Peter LarssonHelsingborg, Sweden
For example, the query looks likeUSE NorthwindGOCREATE VIEW EXPORT_ORDERSASSELECT 1 AS ROW_ORDER, 'HEADER ' + CONVERT(char(25),GetDate()) AS Data_LineUNION ALLSELECT 2 AS ROW_ORDER, COALESCE(CONVERT(char(15),OrderID),'') + COALESCE(CustomerID,'') + COALESCE(CONVERT(char(15),EmployeeID),'') + COALESCE(CONVERT(char(25),OrderDate),'') + COALESCE(CONVERT(char(25),RequiredDate),'') + COALESCE(CONVERT(char(25),ShippedDate),'') + COALESCE(CONVERT(char(15),ShipVia),'') + COALESCE(CONVERT(char(15),Freight),'') + COALESCE(CONVERT(char(80),ShipName),'') + COALESCE(CONVERT(char(120),ShipAddress),'') + COALESCE(CONVERT(char(30),ShipCity),'') + COALESCE(CONVERT(char(30),ShipRegion),'') + COALESCE(CONVERT(char(20),ShipPostalCode),'') + COALESCE(CONVERT(char(30),ShipCountry),'') AS Data_Line FROM OrdersUNION ALLSELECT 3 AS ROW_ORDER, 'TRAILER ' + CONVERT(char(25),GetDate()) + CONVERT(char(15),COUNT(*)) AS Data_Line FROM OrdersGOSELECT Data_Line FROM EXPORT_ORDERS ORDER BY ROW_ORDEREXEC master..xp_cmdshell 'bcp Northwind.dbo.EXPORT_ORDERS out C:\Orders.txt -S<servername> -c -T'GODROP VIEW EXPORT_ORDERSGO |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-30 : 06:26:14
|
Alter your view as thisALTER VIEW EXPORT_ORDERSASSELECT 1 AS ROW_ORDER, 'HEADER ' + CONVERT(char(25),GetDate()) AS Data_Line, NULL AS Cust_IDUNION ALLSELECT 2, COALESCE(CONVERT(char(15),OrderID),'') + COALESCE(CustomerID,'') + COALESCE(CONVERT(char(15),EmployeeID),'') + COALESCE(CONVERT(char(25),OrderDate),'') + COALESCE(CONVERT(char(25),RequiredDate),'') + COALESCE(CONVERT(char(25),ShippedDate),'') + COALESCE(CONVERT(char(15),ShipVia),'') + COALESCE(CONVERT(char(15),Freight),'') + COALESCE(CONVERT(char(80),ShipName),'') + COALESCE(CONVERT(char(120),ShipAddress),'') + COALESCE(CONVERT(char(30),ShipCity),'') + COALESCE(CONVERT(char(30),ShipRegion),'') + COALESCE(CONVERT(char(20),ShipPostalCode),'') + COALESCE(CONVERT(char(30),ShipCountry),''), CustomerIDFROM OrdersUNION ALLSELECT 3, 'TRAILER ' + CONVERT(char(25), GetDate()) + CONVERT(char(15), q.cnt), q.CustomerIDFROM ( SELECT CustomerID, COUNT(*) cnt FROM Orders ) q Peter LarssonHelsingborg, Sweden |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-30 : 06:31:42
|
And then use a SP like thisDECLARE @Customers TABLE (CustomerID INT)INSERT @CustomersSELECT DISTINCT CustomerIDFROM Orders---WHERE bla bla blaDECLARE @CustID INTSELECT @CustID = MIN(CustomerID)FROM @CustomersWHILE @CustID IS NOT NULL BEGIN EXEC master..xp_cmdshell 'bcp "SELECT Data_Line FROM EXPORT_ORDERS WHERE CustomerID = ' + cast(@CustID as varchar) + '" queryout C:\Orders' + cast(@CustID as varchar) + '.txt -S<servername> -c -T' SELECT @CustID = MIN(CustomerID) FROM @Customers WHERE CustomerID > @CustID END Peter LarssonHelsingborg, Sweden |
|
|
spidey2005
Starting Member
3 Posts |
Posted - 2006-11-01 : 17:56:51
|
Hi Peso,Sorry for the late reply.Can you explain how the stored procedure works?Cheers,Spidey |
|
|
|
|
|