Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Import/Export (DTS) and Replication (2000)
 Header and Detail Record Release!

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 file
consisting 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|Datereleased
D|value1|value2|Cust_ID|Datereleased
D|value1|value2|Cust_ID|Datereleased
D|value1|value2|Cust_ID|Datereleased
D|value1|value2|Cust_ID|Datereleased
D|value1|value2|Cust_ID|Datereleased
D|value1|value2|Cust_ID|Datereleased
T|8
The problem is each file should have a distinct cust_id.
If the application releases data related to a different cust_id then
I 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 Larsson
Helsingborg, Sweden
Go to Top of Page

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 Larsson
Helsingborg, Sweden



For example, the query looks like

USE Northwind
GO

CREATE VIEW EXPORT_ORDERS
AS
SELECT 1 AS ROW_ORDER,
'HEADER '
+ CONVERT(char(25),GetDate()) AS Data_Line
UNION ALL
SELECT 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 Orders
UNION ALL
SELECT 3 AS ROW_ORDER,
'TRAILER '
+ CONVERT(char(25),GetDate())
+ CONVERT(char(15),COUNT(*)) AS Data_Line
FROM Orders
GO

SELECT Data_Line FROM EXPORT_ORDERS ORDER BY ROW_ORDER

EXEC master..xp_cmdshell 'bcp Northwind.dbo.EXPORT_ORDERS out C:\Orders.txt -S<servername> -c -T'
GO

DROP VIEW EXPORT_ORDERS
GO



Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-10-30 : 06:26:14
Alter your view as this
ALTER VIEW EXPORT_ORDERS
AS

SELECT 1 AS ROW_ORDER,
'HEADER ' + CONVERT(char(25),GetDate()) AS Data_Line,
NULL AS Cust_ID
UNION ALL
SELECT 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),''),
CustomerID
FROM Orders
UNION ALL
SELECT 3,
'TRAILER '
+ CONVERT(char(25), GetDate())
+ CONVERT(char(15), q.cnt),
q.CustomerID
FROM (
SELECT CustomerID,
COUNT(*) cnt
FROM Orders
) q


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-10-30 : 06:31:42
And then use a SP like this
DECLARE	@Customers TABLE (CustomerID INT)

INSERT @Customers
SELECT DISTINCT CustomerID
FROM Orders
---WHERE bla bla bla

DECLARE @CustID INT

SELECT @CustID = MIN(CustomerID)
FROM @Customers

WHILE @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 Larsson
Helsingborg, Sweden
Go to Top of Page

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
Go to Top of Page
   

- Advertisement -