Convert SSMS .rpt output file to .txt/.csv

旧时模样 提交于 2019-11-30 00:18:50
nevetsvsx

Simple way: In SQL Server Management Studio, go to the "Query" menu & select "Query Options…" > Results > Text > Change "Output Format" to "Comma Delimited". Now, run your query to export to a file, and once done rename the file from .rpt to .csv and it will open in Excel :).

Here is my solution.

  • Use Microsoft SQL Server Management Studio
  • Configure it to save Tab delimited .rpt files: Go to 'Query' > 'Query Options' > 'Results' > 'Text' > 'Output Format' and choose 'Tab delimited' (press OK)

  • Now, when you create a report, use the 'Save With Encoding...' menu, and select 'Unicode' (by default, it's 'UTF8')

  • You can now open the file with Excel, and everything will be in columns, with no escaping nor foreign characters issues (note the file may be bigger due to unicode encoding).

Well with the help of a friend I found my solution: Rpt files are plain text files generated in MS SQL Server Management Studio, but with UCS-2 Little Endian encoding instead of ANSI.

--> In Ultra-Edit the option ‘file, conversion options, unicode to ASCII’ did the trick. The text file reduces from 330meg to 180 meg, Microsoft Query in Excel can now see the columns, and VBA can read the file & process lines*.

P.s. Another alternative would have been to use MS Access (which can handle big results) and connect with ODBC to the database. However then I would have to use Jet-SQL which has fewer commands than the T-SQL of MS SQL Server Management Studio. Apparently one can create a new file as .adp in MS Access 2007 and then use T-SQL to a SQL Server back end. But in MS Access 2010 (on my PC) this option seems not to exist anymore.

PollusB

You can use BCP

Open a command prompt, then type this:

SET Q="select * from user1.dbo.table1"
BCP.EXE %Q% queryout query.out -S ServerName -T -c -t
  • You can use -U -P (instead of -T) for SQL Authentication.
  • Your app have a problem with UNICODE. You can force a code page using -C {code page}. If in doubt, try 850.

  • -t will force tab as field delimiter, you can change it for comma -t,

The nice thing is you can call this directly from your VBA running shell command.

This is the recommended way I see you can do it.


My Source (Answer from DavidAir)

Pick "results to grid" then then right-click on the grid and select "Save Results As..." This will save a CSV.

Actually, there is a problem with that if some values contain commas - the resulting CSV is not properly escaped. The RPT file is actually quite nice as it contains fixed-width columns. If you have Excel, a relatively easy way of converting the result to CSV is to open the RPT file in Excel. This will bring up the text import wizard and Excel would do a pretty good job at guessing the columns. Go through the wizard and then save the results as CSV.

In my case, I execute a query on SSMS (before that press CTRL+SHIFT+F) the result open a window to save it as an rpt file, I couldn´t read it (no Crystal Report install in my computer) so...next time I runned the query I saved it as (all files) set with extension *.txt, and that´s it I was able to read it as text file.

First get your data in .rpt file by using any of above method.

Default .rpt with fixed space column. (262MB)

Comma delimited with Unicode. (52MB) - I used this.

Change file extension to .csv.

Open/Import it in excel and verify data. File type is 'Text Unicode'.

Save it as CSV (Comma Delimited), which reduced size to 25 MB.

I recommend using the "SQL Server Import and Export Wizard" for a couple reasons:

  • The output file will not have a status message at the bottom like a .rpt file does (ie. "(100 rows affected)") which may mess up your data import
  • Ability to specify custom row and column delimiters of a length greater than 1 character
  • Ability to specify custom source to destination mapping (ie. column FirstName can be mapped to first_name in the CSV)
  • Ability to perform a direct transfer to any other database accessible from the SSMS machine
  • Ability to explicitly select your file encoding and locale

It can be accessed by right-clicking on your database in the management studio (you must right-click the database and not the table) and selecting Tasks > Export Data.

When asked for data source you can select the "SQL Server Native Client" and when asked to select a destination you can select "Flat File Destination".

You are then asked to specify a table or query to use.

You can find more info about the tool here:

https://docs.microsoft.com/en-us/sql/integration-services/import-export-data/start-the-sql-server-import-and-export-wizard?view=sql-server-2017

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!