TSQL: create/export a csv file

DECLARE @filepath varchar(250)
DECLARE @query varchar(100)
DECLARE @cmd varchar(500)

SET @filepath = ‘C:\Daten\Testdaten\test_output_file.csv’
SET @query = ‘SELECT Artikelnummer,Matchcode FROM KHKArtikel’ — may be a query_file.sql

SET @cmd = ‘sqlcmd -S”srv-dev-1″ -E -h-1 -d”ElektroReiners20110720″ -Q”‘+ @query +'” -o “‘ + @filepath + ‘” -s”|”‘ — separator is |

EXEC master..xp_cmdshell @cmd

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s