You have two syntax errors that I could see, the first is the use of BETWEEN
AND year(OrderDate) '2005' between '2008'
The correct syntax is
WHERE col BETWEEN value1 AND value2
And your other error and compare the return of your YEAR a> with a varchar, the year returns an int then compare with an int.
The correct one would be
WHERE year(OrderDate) between 2005 and 2008
Some things in your query are unnecessary as Month(convert(int,getdate(),111))
there is no need here to use convert when you can use Month to return the month see that the select below return the same result.
select Month(convert(int,getdate(),111)), Month(getdate())
The same goes for Year.
I do not know the idea of using CURSOR ?? you did it and did not use it at all ...
DECLARE @Year int
DECLARE db_cursor CURSOR FOR
Select distinct Year(OrderDate) as Year
From Sales.SalesOrderHeader
Order by Year(OrderDate)
OPEN db_cursor FETCH NEXT FROM db_cursor INTO @Year
WHILE @@FETCH_STATUS = 0
If your goal is to make an insert with the return data based on a range of YEARS (2005 - 2008) use lool for and avoid making a select unnecessarily.
DECLARE @ano INT = 2005;
WHILE @ano < 2009
BEGIN
{-- todo}
SET @ano = @ano + 1;
END;
Use the variable you have incremented in your loop to select
AND year(OrderDate) = @ano;
in place of your AND year(OrderDate) '2005' between '2008'
Finally, if you just insert it in that range even then just use between and remove from within your CURSOR it will only duplicate your data the way it is.