title: How to make a monthly report table using query in SQL Server stored procedure?
body: <p>I want to make a table like this (first month until the current month) in SQL Server:</p>
  
  <p><a href="https://i.stack.imgur.com/99BTk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/99BTk.png" alt="enter image description here"></a></p>
  
  <p>But I'm struggling with the query.</p>
  
  <p>I have a query that insert into temporary table using a stored procedure, when I execute the stored procedure, it keeps executing the query.</p>
  
  <p>This is my stored procedure:</p>
  
  <pre><code>ALTER PROCEDURE [dbo].[get_dashboard_bulan]
      @month int,
      @year int
  AS
      SET DATEFORMAT DMY
      SET NOCOUNT ON
  BEGIN
      DECLARE @TEMP_TABLE TABLE (bulan int,
                                 NOA int,
                                 OSPENGAJUAN money, 
                                 OSDISETUJUI money, 
                                 NOABANDING int)
  
      DECLARE @firstdate INT = 1,
              @datenow INT= month ((select DATEADD(day, -1, DATEADD(month, @Month, DATEADD(year, @Year - 1900, 0))))), 
              @NOA INT = 0,
              @OSPENGAJUAN MONEY = 0,
              @OSDISETUJUI MONEY = 0,
              @NOABANDING INT = 0
  
      BEGIN TRY
          IF @month = MONTH(GETDATE()) AND @year = YEAR(GETDATE())
          BEGIN
              SET @datenow = MONTH(GETDATE())
          END
  
          WHILE @firstdate &lt;= @datenow
          BEGIN
              SET @NOA = (SELECT COUNT(tiket_id) 
                          FROM m_tiket 
                          WHERE MONTH(created_at) = @firstdate)
              SET @OSPENGAJUAN = (SELECT SUM(plafond) AS plafond 
                                  FROM m_tiket 
                                  WHERE MONTH(created_at) = @firstdate)
              SET @OSDISETUJUI = (SELECT SUM(plafond_disetujui) AS plafond_disetujui 
                                  FROM m_tiket 
                                  WHERE MONTH(created_at) = @firstdate)
              SET @NOABANDING = (SELECT COUNT(tiket_id) 
                                 FROM m_track 
                                 WHERE status_id = 10 AND MONTH(created_at) = @firstdate)
  
              INSERT INTO @TEMP_TABLE 
              VALUES (@firstdate, @NOA, @OSPENGAJUAN, @OSDISETUJUI, @NOABANDING);
  
              SET @firstdate = month(DATEADD(DAY,1,@firstdate))+1
          END
      END TRY
      BEGIN CATCH
      END CATCH
  END
  
  SELECT 
      bulan, noa, 
      IIF(ospengajuan IS NULL, 0, ospengajuan) AS ospengajuan, 
      IIF(osdisetujui IS NULL, 0, osdisetujui) AS osdisetujui, 
      noabanding 
  FROM 
      @TEMP_TABLE
  </code></pre>