Exporting data

Is it possible to export data within a sql query?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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"
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....

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mustish1Author Commented:
Thank You.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.