I think your WHILE
loop is messed up. You are declaring @end_year = 2015
and then saying WHILE (@end_year < 2015)
. I think you meant WHILE (@loop_year < @end_year)
.
With that said, there is no reason to use a loop for this particular case. This should accomplish the same results:
Select count(*) as rows,
month(create_datetime) as month,
year(create_datetime) as year
FROM [table_name]
WHERE year(create_datetime) BETWEEN 1999 AND 2014
GROUP BY month(create_datetime), year(create_datetime)