Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save output of multiple queries into file?

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')
like image 236
Enrico Avatar asked Nov 09 '16 14:11

Enrico


2 Answers

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
like image 147
Nirjhar Vermani Avatar answered Nov 14 '22 23:11

Nirjhar Vermani


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.

like image 28
CapnShanty Avatar answered Nov 14 '22 21:11

CapnShanty