Home » excel » sql server – Pasting Large Amounts of Data to Excel

sql server – Pasting Large Amounts of Data to Excel

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have a query that I run in SQL Server using Management Studio. The results are output to the grid that I then copy and paste to Excel sheet that gets saved in .csv format. I’ve never had any problem doing this before but this one particular query includes a column that contains a large chunk of HTML (several thousand char.). If I try to paste the entire query output into excel it can’t handle that column and the sheet is unusable.

I have to paste everything else and then copy each of the rows individually that contain the HTML and double click the destination cell in Excel to enter in “edit mode”. Is there any way around this?

I have tried: Output to text but no matter what I set the maximum column value to in SSMS it truncates the HTML column. I have tried output to file but the only file choice it gives me is Crystal Reports. Is there a way to either put Excel in edit mode globally so that I can paste the entire thing at once, or to tell SSMS (Express Version) to output to a .csv file?

ETA: I’ve tried right clicking on the grid results and saving as .csv but it still truncates the HTML column. Even though I have “Results to Text” option set at 8K chars. and non XML data set to over 64K in the results to grid option.

How to&Answers:

As @Remy pointed out, have you tried using OPENROWSET:

insert into OPENROWSET('Microsoft.Jet.OLEDB.4.0', 
'Excel 8.0;Database=C:\template.xls;', 
'SELECT * FROM [SheetName$]') 
select * from myTable

— EDIT

Make sure you have Ad Hoc Distributed Queries enabled:

sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ad Hoc Distributed Queries', 1;
GO
RECONFIGURE;
GO

Good luck.

Answer:

Right click the database in SSMS select “Tasks” and then “Export Data …”. Set the SQLServer as a source and an Excel file as destination. On the next step choose “Write a query to specify …” and paste your query. If you need to automate this you can use SSIS.
If you don’t have SSMS or you don’t want to go through the export wizard each time you can use a free tool. Download R-Tag Report Manager from www.r-tag.com , add your query as a SQL Ad-hoc report and export it to excel.

Answer:

In SSMS 2012 (not sure about earlier versions), you can right click on the results and select Save Results As... and then select CSV as your output file.

<a href="https://exceptionshub.com/category/sql-server”>More Answers>