Question : while with union

Can someone tell me how I can do a select with a while and union? I am trying to get the revenue from 2009 until the current year for the month of June. I'm sure this is not right, but here is what I am trying to do. And here is the error I am getting. I can get it to work without the union, but I need it to output into a spreadsheet and the union alows that to happen.

Msg 156, Level 15, State 1, Line 6
Incorrect syntax near the keyword 'union'.
Msg 102, Level 15, State 1, Line 16
Incorrect syntax near 'end'.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
declare @year int, @month int
set @year = 2009
set @month = 06
while @year < 2011
begin
union
select 
	h.ivh_revtype1, 
	@month, @year,
	'Line Haul',
	(select sum(i.ivd_charge)) 
from invoicedetail as i inner join invoiceheader as h on i.ivh_hdrnumber = h.ivh_hdrnumber
where month(h.ivh_deliverydate) = 06 and i.cht_itemcode like 'LH%' and
	year(h.ivh_deliverydate) = @year 
group by h.ivh_revtype1
set @year = @year + 1
end

Answer : while with union

instead of union and while just use

year(h.ivh_deliverydate)  between @yearFrom and @yearTo
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
declare @year int, @month int
set @yearFrom = 2009
set @yearTo = 2011
set @month = 06
begin
select 
	h.ivh_revtype1, 
	@month, @year,
	'Line Haul',
	(select sum(i.ivd_charge)) 
from invoicedetail as i inner join invoiceheader as h on i.ivh_hdrnumber = h.ivh_hdrnumber
where month(h.ivh_deliverydate) = 06 and i.cht_itemcode like 'LH%' and
	year(h.ivh_deliverydate) between @yearFrom and @yearTo 
group by h.ivh_revtype1

end
Random Solutions  
 
programming4us programming4us