title: SQL Cursor within Stored Procedure to populate string variable
body: <p>I have a stored procedure that contains a cursor to loop through SQL records and populates the string which I will use later as my email text. I'm trying to print it out to verify before I can proceed with it but it seems to not populate the string. Here is my stored procedure in SQL Server 2005. </p>
  
  <pre><code>CREATE PROCEDURE [dbo].[spBody] 
  AS
  DECLARE @MyCursor CURSOR
  DECLARE @emailBody nvarchar(max)
  DECLARE @statusName nvarchar(max)
  DECLARE @deptCode nvarchar(max)
  DECLARE @instructors nvarchar(max)
  DECLARE @meetingTime nvarchar(max)
  
  SET @MyCursor = CURSOR FAST_FORWARD For
  Select StatusName, DeptCode, Instructors, Description from MyTable where StatusID = (select CAST(value AS INT) from Table2 where ConfigOption = 'RequiredStatus')
  
  Open @MyCursor
  FETCH NEXT FROM @MyCursor INTO @statusName, @deptCode, @instructors, @meetingTime
  
  
  WHILE @@FETCH_STATUS = 0
  
  BEGIN
   SET @emailBody = @emailBody + @statusName + ' ' + @deptCode + ' ' + @instructors + ' ' + @meetingTime 
   FETCH NEXT FROM @MyCursor INTO @statusName, @deptCode, @instructors, @meetingTime
  
  END
  CLOSE @MyCursor
  Print @emailBody
  
  DEALLOCATE @MyCursor 
  </code></pre>