- Introduction
- Executing a Stored Procedure
- Metadata Representation
Executing a Stored Procedure
Using the TSQLDataset component, you can execute a stored procedure that doesn't return a result set. To do this, set the TSQLDataSet.CommandType property to ctStoredProc. The TSQLDataset.CommandText property will become a drop-down that displays a list of stored procedures in the database. You must select one of the stored procedures that doesn't return a result set. For example:
CREATE PROCEDURE ADD_COUNTRY ( ICOUNTRY VARCHAR(15), ICURRENCY VARCHAR(10) ) AS BEGIN INSERT INTO COUNTRY(COUNTRY, CURRENCY) VALUES (:iCOUNTRY, :iCURRENCY); SUSPEND; END
This procedure is a simple INSERT statement into the country table. To execute the procedure, you must call the TSQLDataset.ExecSQL() method as shown in the following code:
procedure TForm1.btnAddCurrencyClick(Sender: TObject); begin sqlDSAddCountry.ParamByName('ICountry').AsString := edtCountry.Text; sqlDSAddCountry.ParamByName('ICURRENCY').AsString := edtCurrency.Text; sqlDSAddCountry.ExecSQL(False); end;
The first thing you must do is to set the parameter values. Then, by calling ExecSQL(), the specified procedure will be executed with the values you've added. Note that ExecSQL() takes a Boolean parameter. This parameter is used to determine whether any parameters need to be prepared. By default, this parameter should be true.