Can't paste data from SQL into Excel - sql

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

Related

Excel SQL data import parameter issue

I tried to find the answer myself but not knowing how to word the question caused problems :).
I have an excel workbook that I use to pull data from SQL Server 2005 using a stored procedure that accepts a parameter. I am using Microsoft Query in Excel. I am trying to get Excel to grab the parameter from a cell so that the users will not have to edit the connection. If I were to do this as SQL, I would replace the value with a ? and point it to a cell without issue. Since this is a SP, I get a strange response.
This works:
exec [GTI_mainframe].[proc_mf_forecast_authorizations] .07
This:
exec [GTI_mainframe].[proc_mf_forecast_authorizations] ?
Gives me the following message box:
[Microsoft][ODBC SQL Server Driver]Invalid parameter number
I control the SP and the excel workbook so can impliment what I need to. I had one person suggest a vba approach to reference the cell value directly. I could do that but my choice would be to not have to do it in macros. I would love to just be able to use the "refresh all" from the data tab in the ribbon bar. Any thoughts?
I recommend you XLReport; it allows you to connect to almost any database, create queries with parameters.
You can download the trial here.

SQL Server Management Studio show complete content of field varchar(max)

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/

Saving results with headers in SQL Server Management Studio

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.

Is there any straightforward way to output text files (or CSV) from SQL Server?

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.

Is there a way to print out the results of a table query in SQL Server Management Studio

I could have sworn I've done this before but cannot figure it out again. The context menu doesn't have a print option and I don't see one in the toolbar menu.
NO - just select columns - copy and paste in Excel - next print the way how you want
I actually use 'Access Data Projects' for this purpose, it makes printing MUCH nicer, and then you can also copy / paste from ADP into Outlook without going through Excel.