Quantcast
Viewing latest article 2
Browse Latest Browse All 4

Answer by Usman Butt

@tombiernacki I would not recommend a Loop to acheive this. SQL Server is not build for such programming. Also, for your own good please always try to build a solution using a set based approach. Your problem can be easily done with a Recursive cte OR using cteTally numbers table (Arbitrary numbers table). Using cteTally numbers table this is how it can be done Declare @year INT, @id INT SELECT @year = 2012, @id = 2 ;WITH CTETally AS ( SELECT TOP(@id+1) N = ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) FROM (VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) d (n), (VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) e (n), (VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) f (n) ) SELECT @year + N - 1 FROM CTETally ORDER BY N Hope it helps.

Viewing latest article 2
Browse Latest Browse All 4

Trending Articles