Using Stored Procedures With Delphi and InterBase
Using Stored Procedures With Delphi and InterBase
IBPhoenix Development
http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_using_sps 17/11/2009
Using Stored Procedures with Delphi and InterBase Page 2 of 7
Downloads
Main Downloads
InterBase®
Contributed
Research
Research Center
Articles
Examples
FAQs
Search
Search KnowledgeBase Application Maintenance also becomes an issue when using DSQL. If a
Search Web Site change is made in database structure or in the applications accessing that
Search Mers List data, each of the applications must be manually modified and re-tested to
Partners be sure the modifications have been made properly. If there are a fair
Partners number of applications utilizing the same data, which is common, each
application will require modification and rebuilding. This can severely impact
This Site Uses: users, especially when the application is uses to process day-to-day
information.
Stored Procedures
Stored procedures also provide compelling reasons for use. When a stored
procedure is added to the database it is decoded and compiled becoming a
semi-permanent addition to your database definition. Since a procedure is
only decoded and compiled once, upon adding it to the database, each
subsequent request can avoid going through those performance robbing
steps. This becomes especially apparent when large DSQL statements are
converted to stored procedures.
Network traffic is also reduced, a key for large systems, since only the
procedure name and parameters are being passed across the network.
There are a number of factors that lead to the decision of when and where
to use stored procedures. There are a few questions that can be asked
about the application which can help guide in the decision to use stored
procedures or DSQL. The following questions are meant as guidelines, not
http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_using_sps 17/11/2009
Using Stored Procedures with Delphi and InterBase Page 3 of 7
If you have more that one application that will need access to the same
data, creating a stored procedure would keep you from having to duplicate
the DSQL code in each of the application that need this information.
The Larger and more complex the DSQL statement is the more network
traffic it incurs and the longer it takes to be translated by the server into
executable statements.
InterBase version 4.0 added the capability to create and execute stored
procedures. This capability was also extended to the Local InterBase Server
(LIBS), currently included with Delphi. From within Delphi you can develop
applications using LIBS, taking advantage of the capability of stored
procedures and move the same code over to an InterBase Workgroup (NT,
NLM), or UNIX Server. InterBase stored procedures can be broken into two
categories, executable and select procedures.
http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_using_sps 17/11/2009
Using Stored Procedures with Delphi and InterBase Page 4 of 7
Syntax:
SELECT * FROM <stored proc name> (<stored proc parameters>, ..)
Example:
SELECT * FROM MAIL_LABEL(1004)
Delphi has a number of mechanisms for using stored procedures. The two
key components are TStoredProc and TQuery. Each component has specific
advantages depending on the database they are being used with. For our
purposes we will examine which Delphi components to use and when, with
regard to InterBase procedures
http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_using_sps 17/11/2009
Using Stored Procedures with Delphi and InterBase Page 5 of 7
StoredProc1.Prepare;
StoredProc1.ParamByName('LAST_NAME').AsString := Edit1.Text;
StoredProc1.ExecProc;
This approach is somewhat similar to using views but allows for greater
flexibility. A developers can have greater control over what the user can
gain access to and when. With stored procedures, permissions can be
modified dynamically depending on the operation being performed.
InterBase Security
Adding users to InterBase can be done easily through the Server Manger
utility. If it is necessary to create groups, it can only be done on the server
http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_using_sps 17/11/2009
Using Stored Procedures with Delphi and InterBase Page 6 of 7
Controlling Access
Stored procedures can improve data security by providing data access only
through the execution of the procedure. The user or group has all rights to
Tables and Views revoked so they can in no way view or edit their contents.
Instead the user gains access to the data by executing a stored procedure
which returns the desired data-set.
Rather than providing the user with access to the tables, appropriate access
rights are granted to the procedure. The user is then granted execute rights
to the procedure. At no time does any user have direct access to a table or
the data contained within it. Below is a sample procedure providing SELECT
access to the data.
http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_using_sps 17/11/2009
Using Stored Procedures with Delphi and InterBase Page 7 of 7
The procedure in code example 6 returns to the caller all the fields in the
Employee table except for the Salary field. This technique can be used to
secure sensitive data.
Once procedures have been created you can REVOKE individual user's rights
from the tables you wish to control access on.
With the users' rights revoked each stored procedure must be granted the
appropriate access rights to the tables they interact with. If a procedure
does not have the correct level of access to a table, the procedure will fail.
This approach removes the responsibility for gaining access to the tables
from the user and places it in the hands of each procedure. New users can
be added without a great deal of administration overhead.
http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_using_sps 17/11/2009