Monday, May 11, 2009

Export to a Text file in SQL Server Express

Export to a Text file in SQL Server Express: "the typical query could be
SET NOCOUNT ON;
USE AdventureWorks;
GO
SELECT RIGHT(REPLICATE(' ' , 6) + CONVERT(varchar, AddressID), 6 ) AS AddressID,
CONVERT(varchar(10), ModifiedDate, 121) AS ModifiedDate
FROM Person.Address
ORDER BY ModifiedDate;
--<--------
AddressID ModifiedDate
--------- ------------
61 1996-07-24
234 1997-02-19
224 1997-12-05
11387 1997-12-29
1997-12-29
( result abridged ) and want the results to be output to c:\myRes.txt..
create a file to store your query like

SELECT RIGHT(REPLICATE(' ' , 6) + CONVERT(varchar, AddressID), 6 ) AS AddressID,
CONVERT(varchar(10), ModifiedDate, 121) AS ModifiedDate
FROM Person.Address
ORDER BY ModifiedDate;


and execute, from the command line, the SQLCmd utility like
c:\..\>sqlcmd -E -S(Local)\SQLExpress -dAdventureWorks -iC:\myQuery -oC:\myRes.txt
this will output the result as
AddressID ModifiedDate
--------- ------------
61 1996-07-24
234 1997-02-19
224 1997-12-05
11387 1997-12-29
1 1998-01-04
( result abridged )
you can play with the other SQLCmd parameters as wel, like -h for repeted headers, -s for col separator and the like...
regards"

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.

More Important Links

Followers