Page 1 of 1

How to use TMSScript.OnError

Posted: Mon 05 Dec 2016 14:46
by brace
I use TMSScript to run scripts in a loop.

As I upgrade my applciation to version N+1 there is a number of scripts like
010.sql
011.sql
...
025.sql

that are run sequentially with TMSScript.Execute.
Up to now I used OnError to handle errors like this

Code: Select all

scrUpgrader: TMSScript;
[...]
// this is how the OnError eventhandler is setup (one line of code)
procedure TfrmUpgrader.scrUpgraderError(Sender: TObject; E: Exception;
  SQL: string; var Action: TErrorAction);
begin
  Action := eaFail;
end;

// this is how i execute the script in a loop (i exclude the loop code):
        try
          scrUpgrader.SQL.Clear;
          scrUpgrader.SQL.LoadFromFile(ScriptName);
          scrUpgrader.Execute;
        except
          On E:Exception do
          begin
            Result := False;
            
            msg := Format('Error in script %s'+#13#10,[i]);
            memLog.Lines.Append(msg);
        end;
Now I would like to distinguish between exceptions that are due to a real Sql Server issue (like violation of FK. truncation errors, ...) and other exceptions (system out of resources or any other issue that can make a query fail).

Please suggest me how to do.

Should I filter by Exception type or do something in OnError?

Thanks

Re: How to use TMSScript.OnError

Posted: Fri 09 Dec 2016 14:18
by azyk
You can handle SQL Server errors by their error code in the try ... except block using EMSError exceptions. The error code value is in the EMSError.MSSQLErrorCode property.
For example:

Code: Select all

  except
    on E:EMSError do
...
    on E:Exception do