Question : Looping in SQL Stored Procedure

I have a small stored procedure that populates a chart showing seasonal distributions.  My problem is that if there are no records for a given month, that month is skipped and the visual interpretation is off.  There must be a way to loop through from 1 through 12 and if no record exists assign a value of 0 for that month's TotalCount as well as the looping variable for the value of oMonth so the Case statement can take effect.  Can anyone give me a few hints on how to get started?  Thanks.

SELECT SUM(spCount) AS TotalCount,
                  CASE oMonth
                        WHEN 1 THEN 'Jan'
                        WHEN 2 THEN 'Feb'
                        WHEN 3 THEN 'Mar'
                        WHEN 4 THEN 'Apr'
                        WHEN 5 THEN 'May'
                        WHEN 6 THEN 'Jun'
                        WHEN 7 THEN 'Jul'
                        WHEN 8 THEN 'Aug'
                        WHEN 9 THEN 'Sep'
                        WHEN 10 THEN 'Oct'
                        WHEN 11 THEN 'Nov'
                        WHEN 12 THEN 'Dec'
                        ELSE ''
                  END
                  AS MyMonth
      FROM         BIRDS_vw_SpeciesByMonth
      GROUP BY specID, oMonth
      HAVING     (specID = @specID)
      ORDER BY oMonth

Answer : Looping in SQL Stored Procedure

It might help to post the structure of BIRDS_vw_SpeciesByMonth and if possible a small subset of data from that view (ie select top 10 * from BIRDS_vw_SpeciesByMonth).

If I'm interpreting your intention correctly you're using IF EXISTS and ELSE to try to put a 0 in place instead of a NULL?  Can you use ISNULL to resolve that condition instead?
Random Solutions  
 
programming4us programming4us