title: SqlDataAdapter.FillSchema and conditional SQL statements
body: <p>I'm creating application that <strong>needs to retrieve SQL statement's schema</strong> (actually the returned table's schema): the set of column names and corresponding SQL types.
  The basic implementation is the following:</p>
  
  <pre><code>DataTable table = new DataTable();
  SqlDataAdapter sda = new SqlDataAdapter(sqlCommand);
  sda.FillSchema(table, SchemaType.Source);
  </code></pre>
  
  <p>After that I parse the columns info of the DataTable.
  Everything works fine until I try to use conditional statement in SQL and return data with different schema based on that condition. For example:</p>
  
  <pre><code>CREATE TABLE TestDataTable(Column1 int, Column2 nvarchar(100))
  
  IF EXISTS(SELECT * FROM TestDataTable)
  BEGIN 
    SELECT Column1 FROM TestDataTable WHERE Column1&gt;2 
  END
  ELSE 
  BEGIN 
    SELECT Column2 FROM TestDataTable WHERE Column1&gt;2 
  END
  </code></pre>
  
  <p>The result is always Column1 in retrieved schema, not depending on condition in IF statement (when I invert it the result still the same).
  Once more SQL-indicator is CASE statement in the SELECT clause - if they return different columns based on some condition, we get even more surprising results.</p>
  
  <p>I tried different variants and options but every of them has it's own limitations:</p>
  
  <ol>
  <li><em>SqlDataAdapter.Fill();</em> - IF statemt evaluated correctly, but no way to get string types lengths</li>
  <li><em>sqlCommand.ExecuteReader(CommandBehavior.SequentialAccess);    SqlDataReader.GetSchemaTable();</em> - IF statemt evaluated correctly, but no way to get string types lengths</li>
  <li><em>sqlCommand.ExecuteReader(CommandBehavior.SchemaOnly);    SqlDataReader.GetSchemaTable();</em> - No way to get string types lengths. In addition it also returns schema assuming that every condition evaluates to True.</li>
  </ol>
  
  <p>I found discussions of how the SqlDataAdapter.FillSchema() method works and looks like it is really complicated and generates some metadata-request SQL. So I'm really surprised that it works correctly even with dynamically generated SQL, like</p>
  
  <pre><code>DECLARE @selectPart1 nvarchar(100)
  SET @selectPart1=N'SELECT Column1'
  DECLARE @selectPart2 nvarchar(100)
  SET @selectPart2=N'FROM TestDataTable'
  DECLARE @selectQuery nvarchar(200)
  SET @selectQuery = @selectPart1 + ' ' + @selectPart2
  
  EXECUTE sp_executesql @selectQuery"
  </code></pre>
  
  <p><strong>But why can't it evaluate the IF condition, if it successfully evaluates the SET statements?</strong></p>
  
  <p><strong>PS:</strong> I understand that statements that return different data schema based on some condition is something unusual and hard to handle and it is definitely a bad-practice, but the SQL we'll get is user-provided and we should at least detect such inconsistent cases.</p>
  
  <p><strong>As a workaround, a way to detect such cases (to show appropriate message to user) is also acceptable.</strong></p>