15

I have a SQL query that I have to run against multiple (15) tables in SQL Server Management Studio.

Is it possible to save the result of multiple queries into a file? (.txt, excel sheet?)

Using union is not possible because not all tables have an equal amount of columns.

The queries look somewhat like this

select *
from tableA
where main_id in (select id from maintable where date is null and status ='new')

select *
from tableB
where main_id in (select id from maintable where date is null and status ='new')

select *
from tableC
where main_id in (select id from maintable where date is null and status ='new')

select *
from tableD
where main_id in (select id from maintable where date is null and status ='new')

select *
from tableE
where main_id in (select id from maintable where date is null and status ='new')
6
  • Which dbms are you using? (ANSI SQL knows nothing about files.) Commented Nov 9, 2016 at 14:55
  • I'm running the query in MS sql server Commented Nov 9, 2016 at 14:56
  • Possible duplicate of spool output to particular file from command prompt Commented Nov 9, 2016 at 14:57
  • I'm not using a command prompt. Commented Nov 9, 2016 at 14:58
  • Are you using SSMS? Commented Nov 9, 2016 at 15:02

3 Answers 3

14

Try below:-

  1. Open SQL Server Management Studio
  2. Go to Tools > Options > Query Results > SQL Server > Results To Text
  3. Then on right hand side, change output format to comma delimited.
  4. Run your query and then right click on results and click save results to file.
  5. Once done rename the file from .rpt to .csv
Sign up to request clarification or add additional context in comments.

2 Comments

It doesn't really give a correct XML sheet (everything is in the same tab) But hey.. It's something. thanks :) +1
I don't see this answer have solution for 'save multi table to one file'
9

Go to the Query menu > "Results to"... and then pick "to file" or whichever you want. enter image description here Change rpt extention to csv.

Be sure to re-run your queries.

2 Comments

I don't see this answer have solution for 'save multi table to one file'
It works, but even if you change the extension to CSV, you get a text file with columns separated by whitespace.
0

If you want multiple queries result to a single file, you can follow below steps:

  1. Create view for every SQL query you have got. This is just for loading purpose.
  2. You can use Import-Export wizard in SQL Server Management Studio. Right click database > Tasks > Export Data.
  3. In the Wizard choose SQL Server database as source and Excel file as destination.
  4. Choose export multiple tables and select views as the source, and in the target excel, a separate sheet will be mentioned as the destination in the excel file.
  5. Go to next steps in the wizard and Finish the wizard
  6. Now the views data will be loaded to separate sheets in the target excel
  7. Now, you can remove the views, as you dont need them

all the above things can be done inside SSMS.

There are many other options to load data from Multiple ways to export data from SSMS into separate files

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.