0% found this document useful (0 votes)
79 views7 pages

01 Stored Procedures

This document describes how to work with stored procedures in MySQL using the TMyStoredProc component. It covers input, output, and input/output parameters as well as using stored functions and returning result sets.

Uploaded by

nagat4r
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views7 pages

01 Stored Procedures

This document describes how to work with stored procedures in MySQL using the TMyStoredProc component. It covers input, output, and input/output parameters as well as using stored functions and returning result sets.

Uploaded by

nagat4r
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

MyDAC

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:

CREATE PROCEDURE InsertDept (


IN p_deptno INT,
IN p_dname VARCHAR(14) ,
IN p_loc VARCHAR(13)
)
BEGIN
INSERT INTO dept(deptno, dname, loc) VALUES(p_deptno, p_dname, p_loc) ;
END;

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:

call InsertDept( 10,'ACCOUNTING' ,'NEW YORK' );

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;

// choose a stored procedure name to execute


sp.StoredProcName := 'InsertDept' ;

// build a query for a chosen stored procedure based on the Params and StoredP
sp.PrepareSQL;

// assign parameter values


sp.ParamByName( 'p_deptno' ).AsInteger := 10;
sp.ParamByName( 'p_dname' ).AsString := 'ACCOUNTING' ;
sp.ParamByName( 'p_loc').AsString := 'NEW YORK' ;

// execute the stored procedure


sp.Execute;
finally
sp.Free;
end;
end;

[C++Builder]

{
TMyStoredProc* sp = new TMyStoredProc(NULL);
try
{
// con is either TMyConnection or TMyEmbConnection already set up
sp->Connection = con;

// choose a stored procedure name to execute


sp->StoredProcName = "InsertDept" ;

// build a query for chosen stored procedure based on the Params and StoredPro
sp->PrepareSQL();

// assign parameter values


sp->ParamByName( "p_deptno" )->AsInteger = 10;
sp->ParamByName( "p_dname" )->AsString = "ACCOUNTING" ;
sp->ParamByName( "p_loc")->AsString = "NEW YORK" ;

// execute the stored procedure


sp->Execute();
}
__finally
{
sp->Free();
}
}

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:

CREATE PROCEDURE CountDept (


OUT cnt INT
)
BEGIN
SELECT count(*) FROM dept into cnt;
END;

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;

// choose a stored procedure name to execute


sp.StoredProcName := 'CountDept' ;

// build a query for chosen stored procedure based on the Params and StoredPro
sp.PrepareSQL;

// execute the stored procedure


sp.Execute;

// show the value of the output parameter


ShowMessage(IntToStr(sp.ParamByName( 'cnt').AsInteger));
finally
sp.Free;
end;
end;

[C++Builder]
{
TMyStoredProc* sp = new TMyStoredProc(NULL);
try
{
// con is either TMyConnection or TMyEmbConnection already set up
sp->Connection = con;

// choose a stored procedure name to execute


sp->StoredProcName = "CountDept" ;

// build a query for chosen stored procedure based on the Params and StoredPro
sp->PrepareSQL();

// execute the stored procedure


sp->Execute();

// show the value of the output parameter


ShowMessage(IntToStr(sp->ParamByName( "cnt")->AsInteger));
}
__finally
{
sp->Free();
}
}

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:

CREATE PROCEDURE GiveBonus (INOUT sal FLOAT)


BEGIN
SET sal = sal * 1.05;
END

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;

// choose a stored procedure name to execute


sp.StoredProcName := 'GiveBonus' ;
// build a query for chosen stored procedure based on the Params and StoredPro
sp.PrepareSQL;

// assign parameter values


sp.ParamByName( 'sal').AsFloat := 500.5;

// execute the stored procedure


sp.Execute;

// show the value of the input/output parameter


ShowMessage(sp.ParamByName( 'sal').AsString);
finally
sp.Free;
end;
end;

[C++Builder]

{
TMyStoredProc* sp = new TMyStoredProc(NULL);
try
{
// con is either TMyConnection or TMyEmbConnection already set up
sp->Connection = con;

// choose a stored procedure name to execute


sp->StoredProcName = "GiveBonus" ;

// build a query for chosen stored procedure based on the Params and StoredPro
sp->PrepareSQL();

// assign parameter values


sp->ParamByName( "sal")->AsFloat = 500.5;

// execute of the stored procedure


sp->Execute();

// show the value of the input/output parameter


ShowMessage(sp->ParamByName( "sal")->AsString);
}
__finally
{
sp->Free();
}
}

Using MySQL Stored Functions


The tasks described abo ve can be per formed using st ored functions. F or example, the following st ored
function returns the bonus salar y like the GiveBonus stored procedure:

CREATE FUNCTION GiveBonusFunc (sal FLOAT)


RETURNS FLOAT
BEGIN
RETURN sal * 1.05;
END

This function ma y be executed as follo ws:

select GiveBonusFunc( 500.5);

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;

// choose a stored function name to execute


sp.StoredProcName := 'GiveBonusFunc' ;

// build a query for chosen stored procedure based on the Params and StoredPro
sp.PrepareSQL;

// assign parameter values


sp.ParamByName( 'sal').AsFloat := 500.5;

// execute the stored procedure


sp.Execute;

// show the returned value


ShowMessage(sp.ParamByName( 'result' ).AsString);
finally
sp.Free;
end;
end;

[C++Builder]

{
TMyStoredProc* sp = new TMyStoredProc(NULL);
try
{
// con is either TMyConnection or TMyEmbConnection already set up
sp->Connection = con;

// choose a stored function name to execute


sp->StoredProcName = "GiveBonusFunc" ;

// build a query for chosen stored procedure based on the Params and StoredPro
sp->PrepareSQL();

// assign parameter values


sp->ParamByName( "sal")->AsFloat = 500.5;
// execute of the stored procedure
sp->Execute();

// show the returned value


ShowMessage(sp->ParamByName( "result" )->AsString);
}
__finally
{
sp->Free();
}
}

Note: To retrieve the result returned by the stored function using TMySt oredProc, the 'result' parameter
created automatically should be used.

Returning result sets


Besides scalar v ariables, a st ored procedure can return result sets, i.e. the r esults of a SELEC T statement.
This question is discussed in details in the tut orial "Working With Result Sets Using St ored Procedures".

© 1997-2020 Devart. All Rights Reserved. Request Support DAC Forum Provide Feedback

You might also like