01 Stored Procedures
01 Stored Procedures
Stored Procedures
This tutorial describes how t o insert data into tables using the components. T his tutorial describes how
to work with st ored procedures using the TMyStoredProc component.
1. Requirements
2. General information
3. Input parameters
4. Output parameters
5. Input/output par ameters
6. Using MySQL St ored Functions
7. Returning r esult sets
Requirements
This walkthr ough supposes that y ou know how to connect t o server (tutorials "Connecting To MySQL"
and "Connecting To MySQL Embedded" ), how to create necessar y objects on th e server (tutorial
"Creating Database Objects" ), and how t o insert data to created tables (tutorial "Inserting Data Int o
Tables").
General information
A stored procedure is a database object that consists of a set of SQL statements, gr ouped together,
stored in the database, and run as a un it to solve a specific pr oblem or per form a set of r elated tasks.
Procedures let you combine the ease and flexibility of SQL with the pr ocedural functionality of a
structured programming language. Lar ge or complex pr ocessing that might r equire execution of se veral
SQL statements is mo ved into stored procedures, and all applications call the pr ocedures only.
Objects similar t o stored procedures are stored functions . Almost everything that is true for pr ocedures,
holds for functions as well. The main diff erence between these objects is that function has a r eturn
value, and pr ocedure has not. Also, st ored procedures may have input, output, and input/output
parameters.
Note: stored procedures and stored functions are suppor ted since MySQL 5.0.
Input parameters
Input parameter (IN) is a parameter which v alue is passed int o a stored procedure/function module. The
procedure might modify the v alue, but the modification is not visible t o the caller when the pr ocedure is
returned. The following pr ocedure inserts a new row into the table dept:
It needs to receive the values to be inserted into the new r ecord, and thus the pr ocedure has three input
parameters, corr esponding t o each field of the table. This pr ocedure may be executed as follows:
To execute the Inser tDept stored procedure using the TMySt oredProc compon ent, the following code can
be used:
[Delphi]
var
sp: TMyStoredProc;
begin
sp := TMyStoredProc.Create( nil);
try
// con is either TMyConnection or TMyEmbConnection already set up
sp.Connection := con;
// build a query for a chosen stored procedure based on the Params and StoredP
sp.PrepareSQL;
[C++Builder]
{
TMyStoredProc* sp = new TMyStoredProc(NULL);
try
{
// con is either TMyConnection or TMyEmbConnection already set up
sp->Connection = con;
// build a query for chosen stored procedure based on the Params and StoredPro
sp->PrepareSQL();
Output parameters
Output parameter (OUT) is a parameter which v alue is passed out of the st ored procedure/function
module. Its initial v alue is NULL within t he procedure, and its v alue is visible t o the caller when the
procedure is returned. The following st ored procedure returns the count of r ecords in the table dept:
To execute the CountDept st ored procedure using the TMySt oredProc compon ent, the following code
can be used:
[Delphi]
var
sp: TMyStoredProc;
begin
sp := TMyStoredProc.Create( nil);
try
// con is either TMyConnection or TMyEmbConnection already set up
sp.Connection := con;
// build a query for chosen stored procedure based on the Params and StoredPro
sp.PrepareSQL;
[C++Builder]
{
TMyStoredProc* sp = new TMyStoredProc(NULL);
try
{
// con is either TMyConnection or TMyEmbConnection already set up
sp->Connection = con;
// build a query for chosen stored procedure based on the Params and StoredPro
sp->PrepareSQL();
Input/output parameters
An input/output par ameter (INOUT) is a parameter that functions as an IN or an OUT par ameter, or both.
The value of the IN/OUT par ameter is passed int o the stored procedure/function and a new v alue can be
assigned to the parameter and passed out of the module. An IN/OUT par ameter must be a v ariable, not a
constant. It can be found on both sides of an assignment. In other wor ds, an IN/OUT par ameter beha ves
like an initializ ed variable.
For example, the following st ored procedure returns the salar y with five percents bonus:
To execute the Giv eBonus stored procedure using the TMySt oredProc compon ent, the following code
can be used:
[Delphi]
var
sp: TMyStoredProc;
begin
sp := TMyStoredProc.Create( nil);
try
// con is either TMyConnection or TMyEmbConnection already set up
sp.Connection := con;
[C++Builder]
{
TMyStoredProc* sp = new TMyStoredProc(NULL);
try
{
// con is either TMyConnection or TMyEmbConnection already set up
sp->Connection = con;
// build a query for chosen stored procedure based on the Params and StoredPro
sp->PrepareSQL();
To execute the Giv eBonusFunc stored function using the TMySt oredProc component, the following code
can be used:
[Delphi]
var
sp: TMyStoredProc;
begin
sp := TMyStoredProc.Create( nil);
try
// con is either TMyConnection or TMyEmbConnection already set up
sp.Connection := con;
// build a query for chosen stored procedure based on the Params and StoredPro
sp.PrepareSQL;
[C++Builder]
{
TMyStoredProc* sp = new TMyStoredProc(NULL);
try
{
// con is either TMyConnection or TMyEmbConnection already set up
sp->Connection = con;
// build a query for chosen stored procedure based on the Params and StoredPro
sp->PrepareSQL();
Note: To retrieve the result returned by the stored function using TMySt oredProc, the 'result' parameter
created automatically should be used.
© 1997-2020 Devart. All Rights Reserved. Request Support DAC Forum Provide Feedback