Icon Executing Stored Procedures

Executing stored procedures is accomplished through the ExecProc and Open methods of the TEDBStoredProc component, or by setting the Active property to True. Before executing a stored procedure you must first specify the source database for the procedure. The source database is specified via the DatabaseName property of the TEDBStoredProc component. The actual procedure name is specified in the StoredProcName property.

Setting the DatabaseName Property
You may specify the DatabaseName property using two different methods:

1) The first method is to set the DatabaseName property of the TEDBStoredProc component to the DatabaseName property of an existing TEDBDatabase component within the application. In this case the actual source database being used will come from the Database property. The following example shows how to use the DatabaseName property to point to an existing TEDBDatabase component for the source database:

begin
   with MyDatabase do
      begin
      DatabaseName:='AccountingDB';
      Database:='Accounting';
      Connected:=True;
      end;
   with MyStoredProc do
      begin
      DatabaseName:='AccountingDB';
      StoredProcName:='GetLedgerEntries';
      Active:=True;
      end;
end;

Information The above example does not assign a value to the SessionName property of either the TEDBDatabase or TEDBStoredProc component because leaving this property blank for both components means that they will use the default session that is automatically created by ElevateDB when the engine is initialized. This session is, by default, a local, not remote, session named "Default" or "". Please see the Connecting Sessions topic for more information.

Another useful feature is using the BeforeConnect event of the TEDBDatabase component to dynamically set the Database property before the TEDBDatabase component attempts to connect to the database. This is especially important when you have the Connected property for the TEDBDatabase component set to True at design-time during application development and wish to change the Database property before the connection is attempted when the application is run.

2) The second method is to enter the name of an existing database directly into the DatabaseName property. In this case a temporary database component will be automatically created, if needed, for the database specified and automatically destroyed when no longer needed. The following example shows how to use the DatabaseName property to point directly to the desired database without referring to a TEDBDatabase component:

begin
   with MySession do
      begin
      SessionName:='Remote';
      SessionType:=stRemote;
      RemoteAddress:='192.168.0.2';
      Active:=True;           
      end;
   with MyStoredProc do
      begin
      SessionName:='Remote';
      DatabaseName:='Accounting';
      StoredProcName:='GetLedgerEntries';
      Active:=True;
      end;
end;

Setting the StoredProcName Property
The procedure is specified via the StoredProcName property of the TEDBStoredProc component.

Preparing the Stored Procedure
By default ElevateDB will automatically prepare a procedure before it is executed. However, you may also manually prepare a procedure using the TEDBStoredProc Prepare method. Once a procedure has been prepared, the Prepared property will be True. Preparing a procedure compiles the procedure, opens all referenced tables, and prepares all internal structures for the execution of the procedure. You should only need to manually prepare a procedure when executing a procedure that requires parameters.

Executing the Procedure
To execute the procedure you should call the TEDBStoredProc ExecProc or Open methods, or you should set the Active property to True. Setting the Active property to True is the same as calling the Open method. The difference between using the ExecProc and Open methods is as follows:

MethodUsage
ExecProcUse this method when the procedure specified in the StoredProcName property may or may not return a result set. The ExecProc method can handle both situations.
OpenUse this method only when you know that the procedure specified in the StoredProcName property will return a result set. Using the Open method with a procedure that does not return a result set will result in an EDatabaseError exception being raised with an error message "Error creating table handle".

The following example shows how to use the ExecProc method to execute a procedure:

begin
   with MyDatabase do
      begin
      DatabaseName:='AccountingDB';
      Database:='Accounting';
      Connected:=True;
      end;
   with MyStoredProc do
      begin
      DatabaseName:='AccountingDB';
      StoredProcName='UpdateLedgerEntries';
      Prepare;
      ParamByName('AccountNo').AsString:='00100';
      ExecProc;
      end;
end;

Tracking the Progress of a Procedure
To take care of tracking the progress of the procedure execution, we have provided the TEDBStoredProc OnProgress event. This event will only be fired if the procedure contains manual progress update calls specifically included by the procedure creator.
Image