title: CREATE/ALTER procedure using Delphi
body: <p>I've got a small problem that I could not find an answer to. I need to create/alter some procedures from Delphi. </p>
  
  <p>So this is my code that take the code from a file and tries to execute it.</p>
  
  <pre><code>procedure TfrmMainApp.actRulezaScriptExecute(Sender: TObject);
  var
    j: Int32;
    sql: string;
    commandFile: TextFile;
    Linie: string;
    affRows: Int32;
    err: string;
  begin
  
    for j := 0 to filesToExecute.Count - 1 do
    begin
      sql := 'USE ' + DBName + #10#13;
      sql := sql + '  GO ' + #10#13;
      AssignFile(commandFile, filesToExecute[j]);
      Reset(commandFile);
      while not EOF(commandFile) do
      begin
        Readln(commandFile, Linie);
        sql := sql + #10#13 + Linie;
      end;
      dmMainScriptRun.ExecuteCommand(sql, err, affRows);
      if err &lt;&gt; '' then
        break;
      Memo1.Lines.Add('Affected rows:' + IntToStr(affRows));
    end;
  
  end;
  
  
  function TdmMainScriptRun.ExecuteCommand(sqlCommand: string; var err: string;
    var affRows: Int32): Boolean;
  begin
    err := '';
    try
      with cmd do
      begin
        CommandText := sqlCommand;
        Execute(affRows, EmptyParam);
      end;
    except
      on E: Exception do
      begin
        err := E.Message;
      end;
    end;
  end;
  </code></pre>
  
  <p>So my file looks like this</p>
  
  <pre><code>CREATE PROCEDURE sp_TestSebi
  AS
  
      print ('testSebi')
  </code></pre>
  
  <p>My command looks like this ( it was taken from the SQL Server Profiler ) </p>
  
  <pre><code>USE Test
  
    GO 
  
  
  CREATE PROCEDURE sp_TestSebi
  
  AS
  
  
      print ('testSebi')
  </code></pre>
  
  <p>Executin this command returns err <code>Incorrect syntax near 'GO'</code> running the script without the GO statement return err <code>CREATE/ALTER PROCEDURE' must be the first statement in a query batch</code> because of the USE clause.</p>
  
  <p>Is there a way I can create a procedure from Delphi? I need the use statement because i'm trying to execute a script on multiple databases.</p>