Jump to content

Export From SQL to flate file


Will76021
 Share

Recommended Posts

I need to export data from SQL to a Flat File with no Headers.

Invoke-Sqlcmd

$dbname = "Sendsuite Live"

$AttachmentPath = "c:\\exportdata\BeckerAccountDataNew.txt"

$QueryFmt= @"

SELECT

dbo.CharPad (s.PurchaseOrderNumber,10,'LEFT',' ')

,dbo.CharPad (s.SalesOrderNumber,10,'RIGHT','0')

,dbo.CharPad (U.UserName ,6,'LEFT',' ')

,dbo.CharPad (P.Reference2,6,'LEFT',' ')

,dbo.CharPad (P.Reference2,6,'LEFT',' ')

,dbo.CharPad (U.UserName,6,'LEFT',' ')

,dbo.CharPad ('',1,'LEFT',' ')

,dbo.CharPad ('MESS',6,'LEFT',' ')

,dbo.CharPad (U.DepartmentName,4,'LEFT',' ')

,dbo.CharPad (P.Reference1,4,'LEFT',' ')

,dbo.CharPad ('||',8,'LEFT',' ')

,dbo.CharPad ('USD',3,'LEFT',' ')

,dbo.CharPad (REPLACE(CONVERT(VARCHAR(10), s.ShipmentDate, 1), '/', ''),6,'LEFT',' ')

,dbo.CharPad (REPLACE(CONVERT(VARCHAR(10), s.ShipmentDate, 1), '/', ''),6,'LEFT',' ')

,dbo.CharPad ('||',6,'LEFT',' ')

,dbo.CharPad (case when s.NoOfPieces IS NULL then '1' else s.NoOfPieces end,9,'LEFT',' ')

,dbo.CharPad (case when s.NoOfPieces IS NULL then '1' else s.NoOfPieces end,9,'LEFT',' ')

,dbo.CharPad (s.TotalCost,11,'LEFT',' ')

,dbo.CharPad ('',1,'LEFT',' ')

,dbo.CharPad ('||',6,'LEFT',' ')

,dbo.CharPad ('||',4,'LEFT',' ')

,dbo.CharPad ('||',20,'LEFT',' ')

,dbo.CharPad (P.Reference4,20,'LEFT',' ')

,dbo.CharPad ('||',2,'LEFT',' ')

,dbo.CharPad ('||',2,'LEFT',' ')

,dbo.CharPad ('||',2,'LEFT',' ')

FROM Shipments as s

left JOIN [Record Identifiers] R on R.ShipmentID = s.ID

left join Packages P on P.ShipmentID = s.ID

left join Users U on U.id = s.ShipForUserID

where P.HistoryStateID <> 4 AND s.ShipmentDate = CONVERT(Date, GETDATE()-3)

"@

Invoke-Sqlcmd -ServerInstance Beware99 -Database $dbname -Query $QueryFmt |Export-Csv $AttachmentPath -NoTypeInformation

This will export it but in a csv and with Headers any help would be apricated.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...