Using Stored Procedures that Return Data Using Parameters
Go Up to Using a Stored Procedure
Stored procedures can be composed to retrieve individual pieces of information, as opposed to whole rows of data, through parameters. For instance, a stored procedure might retrieve the maximum value for a column, add one to that value, and then return that value to the application. Such stored procedures can be used, and the values inspected using either a TIBQuery
or a TIBStoredProc
component. The preferred method for retrieving parameter values is with a TIBStoredProc
.
Retrieving Individual Values with a TIBQuery
Parameter values retrieved via a TIBQuery
component take the form of a single-row dataset, even if only one parameter is returned by the stored procedure. To retrieve individual values from stored procedure parameters using a TIBQuery
component:
- Instantiate a query component.
- In the
TIBQuery.SQL
property, write aSELECT
query that uses the name of the stored procedure instead of a table name. TheSELECT
clause of this query can specify the parameter by its name as if it were a column in a table, or it can simply use the * operator to retrieve all parameter values. - If the stored procedure requires input parameters, express the parameter values as a comma-separated list, enclosed in parentheses, following the procedure name.
- Set the
Active
property toTrue
or invoke theOpen
method.
For example, the InterBase stored procedure GET_HIGH_EMP_NAME
, below, retrieves the alphabetically last value in the LAST_NAME
column of a table named EMPLOYEE.
The stored procedure returns this value in the output parameter High_Last_Name
.
CREATE PROCEDURE GET_HIGH_EMP_NAME
RETURNS (High_Last_Name CHAR(15))
AS
BEGIN
SELECT MAX(LAST_NAME)
FROM EMPLOYEE
INTO :High_Last_Name;
SUSPEND;
END
The SQL statement issued from a TIBQuery
to use this stored procedure would be:
SELECT High_Last_Name
FROM GET_HIGH_EMP_NAME
Retrieving Individual Values with a TIBStoredProc
To retrieve individual values from stored procedure output parameters using a TIBStoredProc
component:
- Instantiate a stored procedure component.
- In the
StoredProcName
property, specify the name of the stored procedure. - If the stored procedure requires input parameters, supply values for the parameters using the
Params
property orParamByName
method. - Invoke the
ExecProc
method. - Inspect the values of individual output parameters using the
Params
property orParamByName
method.
For example, the InterBase stored procedure GET_HIGH_EMP_NAME
, below, retrieves the alphabetically last value in the LAST_NAME
column of a table named EMPLOYEE
. The stored procedure returns this value in the output parameter High_Last_Name
.
CREATE PROCEDURE GET_HIGH_EMP_NAME
RETURNS (High_Last_Name CHAR(15))
AS
BEGIN
SELECT MAX(LAST_NAME)
FROM EMPLOYEE
INTO :High_Last_Name;
SUSPEND;
END
The Delphi code to get the value in the High_Last_Name output parameter and store it to the Text
property of a TEdit
component is:
with StoredProc1 do begin
StoredProcName := 'GET_HIGH_EMP_NAME';
ExecProc;
Edit1.Text := ParamByName('High_Last_Name').AsString;
end;