Exporting data

Is it possible to export data within a sql query?
Who is Participating?
Mark WillsTopic AdvisorCommented:
You can output to File in your SSMS query window by pressing Ctrl+Shift+F

And a variety of other possibilities :)SSMS output to fileYou will be prompted for file name etc

Or right click in top left of results pane and you can save as....
Olaf DoschkeSoftware DeveloperCommented:
I don't know from where you got that idea, but for export as CSV you can use bcp.exe or the import and export wizard. On level of T-SQL you only have the import direction, inserting from txt or from openrowset() calls. All of these possibilities are listed if you google sql server bulk export: https://docs.microsoft.com/en-us/sql/relational-databases/import-export/bulk-import-and-export-of-data-sql-server

With SSIS you also have any ODBC/OLEDB and Text file sources and sinks to use, so also can export data.

With CLR enabled servers you might have further file output options programming in C# procedures, never did that, but in general also any SQL is exporting data, you read it into client side structures depending on client sod language you may get a C# datatable or php array or whatever your client side language retrieves from an odbc request of sql execution. And you typically can easily store that to a txt.

Anyway, for server side export I'd look into bcp.exe as first choice.

Bye, Olaf.
T-SQL has no direct export functions. You can:
1. change default output from SSMS window to file in Options
2. do it in sqlcmd:
sqlcmd -i c:\sql\SQLQuery1.sql -o c:\sql\foo.txt
3. do it in powershell:
Invoke-Sqlcmd -InputFile "c:\sql\SQLQuery1.sql" | Out-File -filePath "c:\sql\foo.txt"
mustish1Author Commented:
Thank You.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.