title: Table with Select Statements, Executing Dynamic SQL and Returning Values
body: <p>I have a select statement that returns a table full of SELECT statements (It goes through every column in every table and creates a select to find if that column contains any bad data).  </p>
  
  <p>I need to take this table full of SELECT statements, execute them, and see if any of them return rows.  If the count(*) > 0, then I want to print out some data.</p>
  
  <p>I was thinking I had to use a cursor, but I have no idea how I would accomplish that.  </p>
  
  <p>Here is my code to get the count of bad data.</p>
  
  <pre><code>SELECT 'SELECT count(*),  '' '+sysobjects.name + ' - ' + syscolumns.name + 
      ' '' FROM ['
           +sysobjects.name + '] WHERE UNICODE(SUBSTRING(['+syscolumns.name+'],Len(['+syscolumns.name+']),1)) = 0' 
           FROM sysobjects 
      JOIN syscolumns ON sysobjects.id = syscolumns.id
      JOIN systypes ON syscolumns.xtype=systypes.xtype
     WHERE sysobjects.xtype='U' and systypes.name IN ('varchar', 'nvarchar')
  ORDER BY sysobjects.name,syscolumns.colid
  </code></pre>
  
  <p>This returns a table with rows like:</p>
  
  <pre><code>SELECT count(*),  ' All_MW_Users - LastName ' FROM [All_MW_Users] WHERE UNICODE(SUBSTRING([LastName],Len([LastName]),1)) = 0
  </code></pre>
  
  <p><strong>I need to execute this select, and if the count(*) > 0, then print the second column.</strong>  I don't want to show anything in the results or messages unless there is data to show.</p>