title: How to save print variable into another variable in SQL Server?
body: <p>Here is my script grab from net</p>
  
  <pre><code>declare @col varchar(255), @cmd varchar(max)
  DECLARE getinfo cursor for 
      SELECT c.name 
      FROM sys.tables t JOIN sys.columns c 
          ON t.Object_ID = c.Object_ID 
      WHERE t.Name = 'tblDeductions' 
      AND c.name like 'fld%name' OPEN getinfo
  FETCH NEXT FROM getinfo into @col
  WHILE @@FETCH_STATUS = 0 
      BEGIN 
          SELECT @cmd = 'IF EXISTS (SELECT top 1 * FROM tblDeductions WHERE [' 
              + @col + '] IS NOT NULL) BEGIN print ''
            ' + @col + ''' end' exec (@cmd)
          FETCH NEXT FROM getinfo into @col
  END CLOSE getinfo
  DEALLOCATE getinfo
  </code></pre>
  
  <p>The above query will display column names that are null values. But I need to save them into another variable with comma separated values.</p>
  
  <p>Thanks in advance.</p>