title: sql question .. how can i dynamically add columns to a sql query result set?
body: <p>I am writing a report to return details about an object ('files') in my database. My application lets users create their own flags for use against file objects. Flags basically consist of a name, then flag instances store a bit value to indicate whether it is set for the parent file object.</p>
  
  <p>I want to write a query that returns one row per file in the database, where the first few columns in the result set contain the file details (id, name, size etc) and the remaining columns are the flag names, with bit values returned to indicate whether the flag is set for the given file row.</p>
  
  <p>Does that make sense? How do i go about writing the query?</p>
  
  <p>Thanks for any help.</p>
  
  <p>Edit: Clarification..</p>
  
  <p>As part of this query, I need to run a sub query that returns the flags that have been created by the user (I do not know these at design time), then incorporate a check for each flag value in the main query to return details about the files. </p>
  
  <p>Simplified schema as follows:</p>
  
  <ul>
  <li>File {Id, Name}</li>
  <li>Flag {Id, Name}</li>
  <li>FileFlags {FileId, FlagId} - a row in this table indicates that the flag is set for the file</li>
  </ul>
  
  <p>I need the query to return a result set with columns something like this:</p>
  
  <p><strong>FileId</strong>     <strong>FileName</strong>     <strong>Flag1Name</strong>     <strong>Flag2Name</strong>  ....   <strong>FlagNName</strong></p>