I am using SQL Server Management Studio.
I wish to save the results of a query to an excel file.
I choose "save as" and then save to CSV file which I can open in excel. All good except I am missing column headers, any ideas how I get them exported?
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
Try the Export Wizard. In this example I select a whole table, but you can just as easily specify a query:
(you can also specify a query here)
Another possibility is to use the clipboard to copy and paste the results directly into Excel. Just be careful with General type Excel columns, as they can sometimes have unpredictable results, depending on your data. CTL-A anywhere in the result grid, and then right-click:
If you have trouble with Excel's General format doing undesired conversions, select the blank columns in Excel before you paste and change the format to "text".
At least in SQL Server 2012, you can right click in the query window and select Query Options. From there you can select Include Headers for grid and/or text and have the Save As work the way you want it without restarting SSMS.
You'll still need to change it in Tools->Options in the menu bar to have new query windows use those settings by default.
The settings which has been advised to change in #Diego's accepted answer might be good if you want to set this option permanently. Permanently means it'll apply to all future query sessions that you open within SQL Server Management Studio(SSMS). This is usually not the case. Also, changing this setting requires restarting SQL Server Management Studio (SSMS). This is again a not-so-nice experience in case there are many unsaved query session windows open and you are in the middle of debugging something.
SQL Server gives a much slicker option of changing this setting on per session basis which is very quick and convenient. I'm detailing the steps below using query options window:
Right click in query editor window. Now, click Query Options... in the context menu as shown below:
Select Grid under Results node in the left navigation pane. Now, check the Include column headers when copying or saving the results check box as shown below:
That's it. Now, your current query session will start honouring your settings with immediate effect. You don't need restarting SSMS. Also, this setting won't be propagated to any future session either. Effectively, changing this setting on a per session basis is much less noisy.
The same problem exists in Visual Studio, here's how to fix it there:
Go to:
Tools > Options > SQL Server Tools > Transact-SQL Editor > Query Results > Results To Grid
Now click the check box to true: "Include column headers when copying or saving the results"
Select your results by clicking in the top left corner, right click and select "Copy with Headers".
Paste in excel.
Done!
Got here when looking for a way to make SSMS properly escape CSV separators when exporting results.
Guess what? - this is actually an option, and it is unchecked by default. So by default, you get broken CSV files (and may not even realize it, esp. if your export is large and your data doesn't have commas normally) - and you have to go in and click a checkbox so that your CSVs export correctly!
To me, this seems like a monumentally stupid design choice and an apt metaphor for Microsoft's approach to software in general ("broken by default, requires meaningless ritualistic actions to make trivial functionality work").
But I will gladly donate $100 to a charity of respondent's choice if someone can give me one valid real-life reason for this option to exist (i.e., an actual scenario where it was useful).
In SQL Server 2014 Management Studio the setting is at:
Tools > Options > Query Results > SQL Server > Results to Text > Include column headers in the result set.
I also face the same issue. When I used right click in the query window and select Query Options. But header rows does not show up in output CSV file.
Then I logoff the server, login again and run the script. Then it worked.
Related
I created code in SQL to get specific data.
However, when I try to paste the results into Excel, I get an error message that says
"Microsoft Excel cannot paste the data."
Has anyone experienced this? If so, do you know what the solution is?
I modified the code to only extract data from one department (instead of 4), to reduce the number of rows from just under 1 million to 153,671 rows, but I still get the same error message. The data has 12 columns.
Your help would greatly be appreciated!
If you are using the keyboard shortcut (ctrl+C, ctrl+V), I guess it's previsible that It can't work, because event if the interface can look like excel, the data is different.
you can do the same using automatic tools that will do the query for you and put the result in a csv / excel document.
If you are using MSSQL, you can look on other MS tools such as SSDT, which is very simple to use and have tools to exchange excel data to (or from) SQL databases
another way is to connect your excel sheet to your database. this is explained on MSDN here
You can't copy and paste that many rows at a time due to cache restraints. Your best bet is export the query result to a csv file and open it in excel. Here's how to do it:
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
From here it'll show your query results as comma-delimited text.
To save the results of your query to a file: Ctrl + Shift + F
There is a way in SQL Server Management Studio to read all the contents of a varchar(max) column when you run a select?
I know that there is an option in Options > Query Execution > SET TEXTSIZE. But I was wondering if are something like when you have a XML file that you can click the cell and a new tab is open with the cell contents.
No - there is no direct way to get full cell data in SSMS grid. SSMS truncates it.
But: SSMSBoost add that I develop does what you need: it allows to copy cell contents 1:1. All bytes, even if there are megabytes !
http://www.ssmsboost.com/Features/ssms-add-in-copy-results-grid-cell-contents-line-with-breaks
Have you tried results to text?
Here's a screenshot / guide:
http://blog.sqlauthority.com/2007/07/19/sql-server-2005-ssms-viewsend-query-results-to-textgridfiles/
I've done a fair amount of tinkering and searching and the best option seems to be either:
sending output to text and copy/pasting into a text file or Excel OR
outputting to a somewhat unorthodox .rpt file, which I'm not sure what you'd do with--opening it in Excel doesn't preserve formatting present in the original output.
For what seems like a pretty common task, I'm surprised there isn't a simpler way to do this.
Can anyone suggest an easier way to go about this than the two methods I outlined?
Oh, and for what it's worth, I'm working on SQL Server 2008.
How about the BCP utility? http://msdn.microsoft.com/en-us/library/ms189569.aspx
Even through the SSMS GUI, it's still a relatively PITA process:
Plan A:
Tools, Options, Query Results, Results to Text
<= Change output format from "fixed columns" to "delimit by tabs"
At that point, you can "Save results to File", and specify a .csv file
Plan B: fire up your favorite scripting language (like vb.net, for example) and just write a program that does the SQL query and writes the .csv. 10 lines, tops :)
Plan C: Yet another approach is to use some external program to do the query and convert the results for you. SQL Server comes with "BCP". You can easily write a .bat file to invoke it:
http://www.simple-talk.com/sql/database-administration/creating-csv-files-using-bcp-and-stored-procedures/
'Hope that helps
Are you using SQL Server Management Studio? If so, when you open a new query window, you can select to send output to a file. Query Menu -> Results To -> Results to File.
Chris, this is actually super-easy.
If your query results get displayed in the grid (as is the default), just right-click on the grid and choose Save Results As....
From within MSSQL Management Studio, you can right-click on your database and select "Tasks" -> "Export Data". It starts-up a wizard that allows you to pick your data source. On the "Destination" page you can select "Microsoft Excel" or "Flat File Destination". The next page in the wizard allows you to specify data from one or more tables, or a custom query to get your data. If you chose "Flat File Destination" before, the next page allows you to set your own delimiters.
Another option is to pull it into Excel from SQL
Run your query and from the Results tab do Ctrl-a to select all, and paste this into Excel.
I want to edit some row values once I get a query output in the result grid.
Its true that we can right click the table and say open table to get an editable table output, but what I want is editable query output, only certain rows matching for my criteria, and edit them in the result grid.
Can this possible inside Microsoft SQL server Management Studio Express?
Yes, This is possible. Right click on the table and Click on Edit Top 200 Rows as show in image below
Then click anywhere inside the result grid, to enable SQL Icon "Show Sql Pane". This will open sql editor for the table you opted to edit, here you can write your own sql query and then you can directly edit the result set of the query.
You can do something similar to what you want. Right click on a table and select "edit top 200 rows" (if you are on SQL Server 2008) or "open table" in SQL Server 2005. Once you get there, there is a button on the top that says "SQL"; when you click on it, it lets you write an SQL statement and you can edit the results of it if you click a cell you want to change.
The way you can do this is by:
turning your select query into a view
right click on the view and choose Edit All Rows (you will get a grid of values you can edit - even if the values are from different tables).
You can also add Insert/Update triggers to your view that will allow you to grab the values from your view fields and then use T-SQL to manage updates to multiple tables.
SSMS - Right Click Results of Edit 200 | Option | Pane | SQL - edit the statement.
The given answers are still valid. No change in SSMS (SQL Server 2016) has been made on that regard.
You can also use the criteria pane, after doing the "Edit Top 200 Rows".
Show criteria pane
Enter some criterion
Edit data directly in the results grid
Additionally, the number of rows for those commands can be customized in your SSMS options.
No. There is no way you can edit the result grid. The result grid is mainly for displaying purposes of the query you executed.
This for the reason that anybody can execute complex queries. Hopefully for the next release they will include this kind of functionality.
I Hope that answer your question.
UPDATE
as you can see correct solution in Learning answer,
In SQL server management 2014 you can
1.click on "Edit Top 200 Rows"
and then
2.clicking on "Show SQL Pane (ctrl+3)"
and
3.removing TOP (200) from select query
Refer to Shen Lance answer there is not a way to edit Result of select query. and the other answers is only for normal select and only for 200 records.
Just choose "Edit Top 200 rows", press Ctrl + 3 in the edit grid region (or click "Show SQL Pane") and edit the query...
But please note that this will work only for the query that doesn't contain "join"
Yes you can edit joined results. (at least in SSMS 2008 R2) After you edit any of the result values in the View that uses joins, you'll need to execute the query again to refresh the results.
You also need to make sure SSMS is configured to allow "Edit All Rows" ... to do this in SSMS - Tools | Options | SQL Server Object Explorer | Commands ... expand the Table and View Options ... put a value of 0 in "Value for Edit Top n Rows command" ... can do this for the select also.
Yves A Martin's response is 100% correct!
Rob
First of all right click the tale select 'Edit All Rows', select 'Query Designer -> Pane -> SQL ', after that you can edit the query output in the grid.
If you need to frequently perform in-cell edits on SQL databases, HeidiSQL works a treat, couldn't be simpler to use, and is free / open source (donations accepted).
Originally written for MySQL, it can now handle SQL Server, and has experimental (as of Aug 2014) PostgreSQL support as well.
Right click on any table in your dB of interest or any database in the server using master if there are joins or using multiple dBs. Select "edit top 200 rows". Select the "SQL" button in the task bar. Copy and paste your code over the existing code and run again. Now you can edit your query's result set. Sherry ;-)
If the query is written as a view, you can edit the view and update values. Updating values is not possible for all views. It is possible only for specific views. See Modifying Data Through View MSDN Link for more information. You can create view for the query and edit the 200 rows as given below:
To be clear: The option "Value for Edit Top Rows command" has nothing to do with the fact if a result set is editable or not. It is just a way to limit the result set.
Editing the result set of a query based on one and only one table is obviously always possible.
The result set of a query based on more than one table is under following condition possible:
You can edit the fields in the result set at once if they belong to one and only one based table in the query! If the fields are Primary Key, then you have to fulfill refresh/"Execute SQL" (Ctrl+R) after each row update, in order to be able to edit a row next time. If the fields are not Primary Key, then you do not need to fulfill refresh/"Execute SQL" (Ctrl+R).
I have tested it on SQL Server 2008 - 2016!
Is there a way to do this if an unsaved tab gets accidentally closed?
I was able to recover a query I was working on after accidentally closing the tab. If you actually ran the query, it should be in SQL Server's query cache. Query the query cache and order the results by creation date. More info on the SQL Server query cache:
Modify a query like this one (found at http://msdn.microsoft.com/en-us/library/ee343986(v=SQL.100).aspx)
SELECT cp.objtype AS PlanType,
OBJECT_NAME(st.objectid,st.dbid) AS ObjectName,
cp.refcounts AS ReferenceCounts,
cp.usecounts AS UseCounts,
st.text AS SQLBatch,
qp.query_plan AS QueryPlan
FROM sys.dm_exec_cached_plans AS cp
CROSS APPLY sys.dm_exec_query_plan(cp.plan_handle) AS qp
CROSS APPLY sys.dm_exec_sql_text(cp.plan_handle) AS st;
to get your desired result. The "st.text" column will have the query that was run on the database server.
I also found at MSDN website that it is not possible to recover these files, but I would give a try to this (it worked for me):
Take a look in the folder C:\Users\YOURUSERIDHERE\Documents\SQL Server Management Studio\Backup Files\Solution1 and choose files for date when machine restarted or crash happened. SQLBlog.com
Take a look in the folder C:\Users\”[your username goes here]“\AppData\Local\Temp\ (this wasn't work for me because my .sql files had 0KB and .tmp files had something, but I couldn't find the way to 'extract' code from these .tmp files). Suppose that sometimes can be helpful, depending on reason of system reboot/crash. ayesamson.com
I'm not sure that there is, but using TimeSnapper can be a help to show what was previously in the window.
I don't believe so. I checked on the msdn website and there's a thread about this and the answer is no.
Navigate to My Documents\SQL Server Management Studio Express\Backup Files\Solution1 you will find the Recovered backlogs.This is the only solution.
1.Take a look in the folder C:\Users\YOURUSERIDHERE\AppData\Local\temp, then sort files by date modified and pick the last .sql that has a size greater than 0 bytes. That worked for me.
Unfortunately SSMS currently does not have the Undo Closed Tab feature. I have created the following Connect Item so Microsoft will hopefully add this in a future version: https://connect.microsoft.com/SQLServer/Feedback/Details/1044403