I need best practice in T-SQL Export data to CSV (with header)
Asked Answered
D

5

9

What I need to do is export data into CSV file using T-SQL.

And I'm very confused about there are many ways can do it, I don't know to choose which one, please help me to confirm the bollowing:

As I know there are about 3 methods, and I want you help me to confirm:

Using Microsoft.Jet.OLEDB.4.0, like this:

INSERT INTO OPENROWSET('Microsoft.Jet.OLEDB.4.0',
                       'Text;Database=C:\Temp\;HDR=Yes;',
                       'SELECT * FROM test.csv')
            (object_id, name)
SELECT object_id, name
  FROM sys.tables;

but this need the csv file is there, and with header

using SQLCMD

command line.

using BCP

Use union, get data and it's column header.

This is all my understanding about T-SQL export to CSV, please help me to confirm.

Is there other way to export to CSV?

Thanks!

Dozy answered 24/9, 2012 at 14:45 Comment(1)
Show us what the CSV file looks like.Fogdog
C
5

You could use a UNION to create a header row, like this:

SELECT 'object_id', 'name'

UNION ALL

SELECT object_id, name
FROM sys.tables
Clinic answered 24/9, 2012 at 15:25 Comment(3)
Sorry, actually, I want a best practice to export data to CSV.Dozy
The best practice is subjective. Use the method that works for you. If you'd like the "most official" solution, that would be to use an SSIS package and schedule it with a SQL Server Agent job.Clinic
Thanks for your reply, using bcp and union can export to csv with header. thanks! Actually, we are moving out from DTS/SSIS...Dozy
T
6

Execute the below command in SQL Server:

EXEC xp_cmdshell 'SQLCMD -S . -d MsVehicleReg2 -Q "SELECT * FROM tempViolationInfo" -s "," -o "O:\result.csv"';
Talca answered 8/5, 2013 at 14:13 Comment(4)
Welcome to Stack Overflow! I think you should explain what the command does. And it would be extremely useful if you detailed each of the commands you're suggesting; but you don't have to.Filiform
Could not find stored procedure 'xp_cmdshell' ?Bodine
@LeeRichardson It is disabled by default in newer versions of SQL Server for safety.Scrivener
You can enter the following command to get information on the commands in a prompt: "sqlcmd -?"Billingsgate
C
5

You could use a UNION to create a header row, like this:

SELECT 'object_id', 'name'

UNION ALL

SELECT object_id, name
FROM sys.tables
Clinic answered 24/9, 2012 at 15:25 Comment(3)
Sorry, actually, I want a best practice to export data to CSV.Dozy
The best practice is subjective. Use the method that works for you. If you'd like the "most official" solution, that would be to use an SSIS package and schedule it with a SQL Server Agent job.Clinic
Thanks for your reply, using bcp and union can export to csv with header. thanks! Actually, we are moving out from DTS/SSIS...Dozy
R
5

Here is the T-SQL way:

INSERT INTO OPENROWSET('Microsoft.ACE.OLEDB.12.0','Text;Database=D:\;HDR=YES;FMT=Delimited','SELECT * FROM [FileName.csv]')
SELECT Field1, Field2, Field3 FROM DatabaseName

But, there's a couple of caveats:

  1. You need to have the Microsoft.ACE.OLEDB.12.0 provider available. The Jet 4.0 provider will work, too, but it's ancient, so I used this one instead.

  2. The .CSV file will have to exist already. If you're using headers (HDR=YES), make sure the first line of the .CSV file is a delimited list of all the fields.

Rhumb answered 22/1, 2014 at 15:15 Comment(0)
P
1

For Ace.OLEDB.12.0 (the new Jet redistributable engine), you can install the 32-bit or 64-bit stand-alone engine, even if you have the "other flavor" already installed, whether on its own, from Access, etc:

use the /passive command-line option:

(32-bit): AccessDatabaseEngine.exe /passive (64-bit): AccessDatabaseEngine_64.exe /passive

In my case, I have 64-bit SQL Express 2008 R2, and had 32-bit Office 12 apps installed (thus, the 32-bit ACE drivers were installed). I installed the 64-bit AccessDatabaseEngine_64.exe, and it's sort of working for me now...

Also, this is assuming you've done the other configuration work:

EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'AllowInProcess', 1 GO

EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'DynamicParameters', 1 GO

sp_configure 'show advanced options', 1; GO RECONFIGURE; GO sp_configure 'Ad Hoc Distributed Queries', 1; GO RECONFIGURE; GO

Patinous answered 28/2, 2014 at 18:42 Comment(0)
P
1

regarding "best practice", there is no best practice. there are several options available, not limited to:

  • in T-SQL with INSERT INTO OPENROWSET(...) SELECT * from [MyTable]...

  • executing BCP, whether from a job step or in T-SQL with xp_cmdshell

  • SSIS packages

  • PowerShell (from a job step, in SQL Server 2008+) or other external script/executable

Patinous answered 28/2, 2014 at 18:47 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.