Search code examples
sql-serverssms

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')

Solution

  • 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