title: Execute a list of queries and store result in a table
body: <p>I am trying to execute the queries resulted from the variable @roles and store the results in the table @table :</p>
  
  <pre><code>    DECLARE @roles NVARCHAR(MAX)
      SET @roles=' '
      DECLARE @table TABLE
     (
      Label NVARCHAR(MAX)
     )
  
      select @roles=@roles+
      'Select '+isnull(er.ColumnName,'*')+' from '+er.SchemaName+'.'+er.TableName+' where '
   +kcu.COLUMN_NAME +'='+er1.ValueId
  
  
           from [Function].[Role] er 
          left outer join INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc on 
              tc.TABLE_NAME=er.TableName and tc.TABLE_SCHEMA=er.SchemaName
          left outer join INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu on kcu.CONSTRAINT_NAME=tc.CONSTRAINT_NAME
          LEFT OUTER JOIN Employee_Role er1 ON er.EntityRoleId = er1.RoleId
          LEFT OUTER JOIN Employee e ON er1.EmployeeId = e.EmployeeId
      where e.EmployeeId=54 AND tc.CONSTRAINT_TYPE='PRIMARY KEY'
  
      INSERT INTO @table
    EXEC sp_executesql @roles;
  </code></pre>
  
  <p>The result of @roles is different query like below : 
  <a href="https://i.stack.imgur.com/ast9V.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ast9V.png" alt="enter image description here"></a></p>
  
  <p>I am getting :</p>
  
  <blockquote>
    <p>( 0 rows affected )</p>
  </blockquote>