Jeff Moden (10/19/2013)
AmarettoSlim (10/19/2013)
Can I ask why you don't want a loop? Recursion via a common table expression (CTE) can be your friend in this case.
DECLARE @fromYear DATETIME, @toYear DATETIME
SELECT @fromYear='1913-01-01', @toYear='1998-01-01'
WITH YearSequence (Year) as
(
SELECT @fromYear AS Year
UNION ALL
SELECT DATEADD(YEAR, 1, Year)
FROM YearSequence
WHERE Year < @toyear
)
SELECT Year FROM YearSequence ORDER BY 1 DESC
Because of the extremely low rowcount, you can't actually see the insidious problem with CTE's that count. Please see the following article...
http://www.sqlservercentral.com/articles/T-SQL/74118/
Also, your code didn't actually run right the first time I tried to run it because of missing semi-colons. You might also want to get out of the habit of using ORDER BY on a column ordinal because that method has been deprecated.
As for why you might want to avoid a loop, do you have a good reason for why you'd want to intentionally write slower code when faster code is easily available and usually easier to write?
Thanks for sharing the article, Jeff. I learn something new everyday and this tops the list for past 24 hours.
born2achieve, don't use my example. Take a few minutes to read the article Jeff wrote, its extremely evident that there are better methods available such as a tally table.