Link to home
Start Free TrialLog in
Avatar of Sam OZ
Sam OZFlag for Australia

asked on

Write multiple sql server count query result to a single excel file

I need to run several   select queries(Sql Server 2014 ) and write the results to an excel sheet

Example
select count(docname)  from TbDocs where plant = 'Plant1'   ------   Write "Plant1" in first column , count in second column of first row
select count(docname) from TbDocs where plant = 'Plant2'   -----   Write "Plant2" in first column , count in second column of second row
select count(docname) from TbDocs where plant = 'Plant3'   -----   Write "Plant3" in first column , count in second column of third row
.. has 20  plants total
(It is desirable, If I can have a header row as first row . But not a must)
Can I have a batch sql script running and writing it to a PlantCount excel file in one shot?
Avatar of Nitin Sontakke
Nitin Sontakke
Flag of India image

I believe, it would much nicer if you query it as follows:

select plant, count(1)
from TbDocs td
group by plant
order by plant

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Jordan Webb
Jordan Webb
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial