title: SQL ExecuteReader throws "Incorrect syntax near GO"
body: <p>I use a T-SQL command which I found <a href="https://msdn.microsoft.com/en-us/library/ms189858.aspx" rel="nofollow noreferrer">here</a> to get the fragmentation of my database tables. When I execute the T-SQL in the Management Studio, everything works. If I use it inside PowerShell, I get the following error (translated from German):</p>
  
  <pre>
  Exception when calling "ExecuteReader" with 0 Argument(s): 
  "Incorrect syntax near 'GO'.
  Incorrect syntax near 'GO'."
  In Zeile:17 Zeichen:6
  +      $Result = $cmd.ExecuteReader()
  +      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : SqlException
  </pre>
  
  <p>This is the part of my script which throws the error</p>
  
  <pre><code>foreach ($table in $tables)
  {
       $Data = New-Object System.Data.DataTable
       $cmd = New-Object System.Data.SqlClient.SqlCommand
       $getFragRate = "
       -- SQL Command von Microsoft um die Index Fragmentation zu überprüfen 
       USE Logik;  
       GO  
       SELECT a.index_id, name, avg_fragmentation_in_percent  
       FROM sys.dm_db_index_physical_stats (DB_ID(N'Logik'), OBJECT_ID(N'$($table)'), NULL, NULL, NULL) AS a  
           JOIN sys.indexes AS b ON a.object_id = b.object_id AND a.index_id = b.index_id;   
       GO  
       "
       $cmd.CommandText = $getFragRate 
       $cmd.Connection = $con
       $Result = $cmd.ExecuteReader()
       $Data.Load($Result)
       $Data
  }
  </code></pre>
  
  <p>Why does this error occur?</p>
  
  <p>The User I use to do this has <code>sysadmin</code>, <code>db_ddladmin</code> and <code>db_owner</code> permission.</p>
  
  <p>Edit: Another T-SQL Command to get all tables of my database worked without a problem from PowerShell.</p>