title: Sql: Incorrect syntax near '('
body: <p>I'm trying to DataBind to a GridView control, but it consistently fails. </p>
  
  <p>Given the following code (sql expanded over multiple lines for readability): </p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
  
  string dsn = "foo";
  string sql = "SELECT * FROM (SELECT F.Project AS 'Project Number', F.Account AS   
               'Account', F.Pd AS Period, F.Incurred AS Totals, C.Project AS 
               'Project Name' FROM Ultron.Final F INNER JOIN Ultron.Custom  
                C ON F.Project = C.Project WHERE F.Project LIKE '61000.003%' AND 
                F.Account NOT LIKE '%-01' AND F.Fy = YEAR(GetDate()) ) Budget PIVOT 
                (SUM(Totals) FOR Period in ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],
                [11],[12]) AS PivotTable";
  using (SqlConnection conn = new SqlConnection(dsn))
  using (SqlCommand cmd = new SqlCommand(sql, conn))
  {
      conn.Open();
      SqlDataReader reader = cmd.ExecuteReader();
      testGrid.DataSource = reader;
      testGrid.DataBind();
  }
  }    
  </code></pre>
  
  <p>I'm sure the error has something to do with the parentheses in the query, but those are required for the query to work. When testing in SSMS the query passes without issue. </p>
  
  <p>How do I restructure this so the code doesn't fail?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I was missing <code>As PivotTable</code> to the end of the SQL query. It works now. Sorry, this was an innocent typo. Thanks for all who responded.</p>