How to use TMSScript.OnError

Discussion of open issues, suggestions and bugs regarding SDAC (SQL Server Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
brace
Posts: 213
Joined: Wed 14 Feb 2007 08:26

How to use TMSScript.OnError

Post by brace » Mon 05 Dec 2016 14:46

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

azyk
Devart Team
Posts: 1057
Joined: Fri 11 Apr 2014 11:47
Location: Alpha Centauri A

Re: How to use TMSScript.OnError

Post by azyk » Fri 09 Dec 2016 14:18

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

Post Reply