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  
  •  Show/Hide All Images
  •  Submit a Form Without Refresh
  •  how to use CFCONTENT to deliver files from a folder outside web root, to a web browser? and loop through a series of fileIDs associated with a DocumentID
  •  Default time exchange deleting emails
  •  How can I find out what type of driver I need for a "other device, Unknown Device" for a nc6400 laptop
  •  PHP.ini file 'display_errors' is set to Off but errors still appear on page?
  •  What is the best Raid stripe size and Windows allocation unit size for large files?
  •  Safari 5 Crashes immedietly
  •  I have designed an website in flash as well in html, if the browser dont have flash then i want them to be directed to the html version, or else they need to directed to flash version
  •  GroupWise 6.0.1 -  Some addresses are undeliverable and have been crossed out
  •  
    programming4us programming4us