Mariano Dabove
Mariano Dabove

Reputation: 43

SQL Query not outputting results

Hi guys I have an issue here with a query that I'm trying to implement...

The issue is the next one...

I want to display the month and year since 01-2014 to the present.

When i run this query it's only display a message that says "Commands run correctly" and doesn't show anything...

Where i am doing it wrong??

Thanks.

Declare @año int, @mes int, @m int

Set @año = Year(Getdate())
Set @mes = Month(Getdate())
Set @m = 0

While @año < 2014 begin
    While @m < 12 begin
        Select @año as año, @m as mes
        set @m = @m + 1
    End
    Set @año = @año - 1
    Set @m = 0
End

Upvotes: 1

Views: 46

Answers (2)

M. Kanarkowski
M. Kanarkowski

Reputation: 2195

You didn't see any output, because of this @año < 2014 (@año = 2019 so it didn't enter the loop). While loop is executed as long as the condition is true. In your example from the beginning it was false. Aditionally,

  1. you set @m = 0, I changed it to 1. A month with number 0 appeared.
  2. you set @m < 12 , I changed it to 13. Because of it December was absent.

    Declare @año int, @mes int, @m int

    Set @año = Year(Getdate())
    Set @mes = Month(Getdate())
    Set @m = 1
    
    While @año > 2014 begin
        While @m < 13 begin
            Select @año as año, @m as mes
            set @m = @m + 1
        End
        Set @año = @año - 1
        Set @m = 1
    End
    

Upvotes: 1

Vasya
Vasya

Reputation: 469

In the beginning you set year to the current year which is greater than 2014 so you loop does not execute

Upvotes: 0

Related Questions