• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 410
  • Last Modified:

Exception - hot to get SQLServer Err. number

I'm using D5 and ADO components to access SQL Server. Is there a way to retrieve SQL server error code, when exception is raised? For example: I use ADOQuery to execute stored procedure, and the procedure is missing, how can I catch this error?
0
pvs001d
Asked:
pvs001d
  • 2
  • 2
  • 2
  • +1
1 Solution
 
pvs001dAuthor Commented:
Adjusted points from 50 to 100
0
 
IPCHCommented:
Can you send part of you code?
0
 
simonetCommented:
You should use the TADOStoredProc component.

After you call the EXecProc method, the parameter @RESPOST (or something like that... it's always the 1st parameter in the TADOStoredProc parameters list) will be filled with the result code of the execution.

Just read its value and you know what happened. The Online Books for SQL Server 7 lists all the error codes that can be returned.

Alex
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
simonetCommented:
Correction:

the parameter is called @RETURN_VALUE and every stored procedure automatically defines this parameter. Remember that you must set its direction as output in the TADOStoredProc's Parameter's editor.

Alex
0
 
pvs001dAuthor Commented:
Answer accepted
0
 
radu_antohiCommented:
The paramater is called @RETURN_VALUE, but the parameter direction is not 'pdOutput', but 'pdReturnValue'.

Delphi Example:

var
  Ret: Integer;
begin
  ADOConnection1.Connected := True;

  ADOStoredProc1.Parameters.Clear;
  ADOStoredProc1.Parameters.CreateParameter('@RETURN_VALUE', ftInteger, pdReturnValue, 0, NULL);

  ADOStoredProc1.ExecProc;

  Ret := ADOStoredProc1.Parameters.ParamByName('@RETURN_VALUE').Value;

  ShowMessage(IntToStr(Ret));
end;
0
 
radu_antohiCommented:
Another way to get the error id is from the exception raised with RAISERROR by the SQL Server.

try
 SP.ExecProc;
except
 on E: EOleException do
 begin
  if (ADOConnection.Errors.Count > 0) then
  begin
   NativeError := ADOConnection.Errors[0].NativeError;
   Description := ADOConnection.Errors[0].Description;
  end
 end;
end;

The best way for error handling is to use both methods (exceptions raised with RAISERROR and RETURN_VALUE), because there are exceptions that are not catched by tke OLEDb provider or by Delphi. I don't know where is the problem.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now