• caojunhe24 (4/21/2015)


    the problem I was facing was that month is not reset after the second loop run.

    so month should be reset in the first loop before second loop starts.

    While (@year <= @Currentyear)

    set @month = 1

    Begin

    While ((@Month <= @Currentmonth and @year= @Currentyear )) or (@Month <= 12 and @year< @Currentyear )

    Thank you for your help tho

    Glad you solved your problem. I would, however, seriously consider reading the article I posted and begin developing SQL that does not use loops. You can make your queries thousands of times faster by switching to a set-based approach to problems like this (something I learned from people like Lynn) 😎

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001