Saving results with headers in SQL Server Management Studio
Tools
> Options
> Query Results
> SQL Server
> Results to Text (or Grid if you want)
> Include columns headers in the result set
You might need to close and reopen SSMS after changing this option.
On the SQL Editor Toolbar you can select save to file without having to restart SSMS
SQL Management Studio exports CSV with header from one server and without header from other server
@destination-data You got me on the right track. I restarted Management Studio and since then I get the save as results as specified in the options. Kinda embarrassing that I didn't think of restarting. Worked fine for days.
Added to mark this as answer.
Exporting from SQL Server to Excel with column headers?
I typically do this by simply click the upper left corner in the results grid, copy, and then paste into Excel. There is one catch, you need to go into options->query results-> SQL Server->results to grid (or text if you want to save to file for import into excel) and turn on include column headers when copying or saving the results. I find this works great.
SSMS :: Copy with Headers and paste to Excel results in more rows in destination
I guess your varchar(max)
causes the problem. The size of a text field in Excel is limited. Following an example:
DECLARE @x VARCHAR(MAX) = '***************************************************************************';
SELECT @x=CONCAT(@x,@x)
SELECT @x=CONCAT(@x,@x)
SELECT @x=CONCAT(@x,@x)
SELECT @x=CONCAT(@x,@x)
SELECT @x=CONCAT(@x,@x)
SELECT @x=CONCAT(@x,@x)
SELECT @x=CONCAT(@x,@x)
SELECT @x=CONCAT(@x,@x)
SELECT @x=CONCAT(@x,@x)
SELECT @x=CONCAT(@x,@x)
SELECT @x=CONCAT(@x,@x)
SELECT @x=CONCAT(@x,@x)
SELECT @x AS Test, 'Test' AS Test2
Copy the result to an excel file and you will get two rows, where the second column (Test2) is only featured in the second row.
Export query result to .csv file in SQL Server 2008
- Open SQL Server Management Studio
- Go to Tools > Options > Query
Results > SQL Server > Results To Text - On the far right, there is a drop
down box called Output Format - Choose Comma Delimited and click OK
Here's a full screen version of that image, below
This will show your query results as comma-delimited text.
To save the results of a query to a file: Ctrl + Shift + F
How can we change the default settings for Save Query result to File in SSMS
SSMS will export the same things defined (and displayed) as if you would run a Result to Text
. In fact, you can define everything under the menu point Result to Text
and it will work for both.
If your missing something here, you can also use BCP
(more infos here) or PowerShell
to export data from an table, view or procedure.
Related Topics
How to Rename a Column in a Database Table Using SQL
How to Drop Multiple Tables in Postgresql Using a Wildcard
SQL Get All Records Older Than 30 Days
Keep Only N Last Records in SQLite Database, Sorted by Date
How to Add a Variable Number of Hours to a Date in Postgresql
How to Escape Non-Format Characters in Oracle's To_Char
Finding Free Slots in a Booking System
"Microsoft.Ace.Oledb.12.0" Cannot Be Loaded In-Process on a 64-Bit SQL Server
Column Conflicts with the Type of Other Columns in the Unpivot List
SQL Server Xp_Delete_File Not Deleting Files
How to Change a Table Name Using an SQL Query
Why Does SQL Server Keep Executing After Raiserror When Xact_Abort Is On
How to Find Records That Are Not Joined
Group Data by the Change of Grouping Column Value in Order
Export from SQL Server 2012 to .CSV Through Management Studio