[[Property:title|Stored Procedures]]
[[Property:weight|6]]
[[Property:uuid|9979af90-07c3-8b20-448e-04454a75c801]]
When sending requests to the database, the request is first parsed then executed. Instead of parsing many times the same requests, i.e. with only changes in expression values, most of RDBMS enable to '''precompile''' requests. These requests can then be executed as routines and are identified by a name and a signature. 

EiffelStore lets you use stored procedures with [[ref:/libraries/store/reference/db_proc_flatshort|DB_PROC]]  class to:
*  [[#execute|Execute]]  a stored procedure. 
*  [[#create|Create]]  a stored procedure. 

==Executing a stored procedure==

To execute a stored procedure:
* Create a [[ref:/libraries/store/reference/db_proc_flatshort|DB_PROC]]  object and load the stored procedure you want to use:
<code>
    procedure: DB_PROC

                ...

            create procedure.make ("UPDATE")
            procedure.load
            if procedure.exists then
                ...
            end
</code>

* Execute the procedure through a [[ref:/libraries/store/reference/db_selection_chart|DB_SELECTION]]  (if a result is expected) or a [[ref:/libraries/store/reference/db_change_chart|DB_CHANGE ]] object (otherwise). 

{{note|Requests with a result ([[ref:/libraries/store/reference/db_selection_chart|DB_SELECTION]] ) or without ([[ref:/libraries/store/reference/db_change_chart|DB_CHANGE]] ) are both abstract '''expressions'''. DB_PROC executes an abstract expression using an object of [[ref:/libraries/store/reference/db_expression_chart|DB_EXPRESSION]]  type, which corresponds to an abstract expression. [[ref:/libraries/store/reference/db_selection_chart|DB_SELECTION]]  and [[ref:/libraries/store/reference/db_change_chart|DB_CHANGE]]  inherits from [[ref:/libraries/store/reference/db_expression_chart|DB_EXPRESSION]] . }}

You can execute your request mostly like a basic one: 
** Create your request.
** Bind request variables. Variables are stored procedure arguments. 

{{note|Take a look at how to [[Query variables|bind variables]]  to a query. }}

** Execute the query through the DB_PROC object.
<code>
    procedure: DB_PROC
    expr: DB_CHANGE

                ...

            procedure.execute (expr)
            expr.clear_all
</code>

** Check for errors and load result if any. 


==Creating a stored procedure==

DB_PROC also enables you to create or drop stored procedures: 
* Use store to create a procedure.
* Use drop to delete one. 
The following example shows how to overwrite a procedure in the database: 
<code>
    procedure: DB_PROC

                ...

            create procedure.make ("NEW_PROCEDURE")
            procedure.load
            if procedure.exists then
                procedure.drop
            end
            procedure.load
            if not procedure.exists then
                procedure.set_arguments (<<"one_arg">>, <<"">>)
                procedure.store ("update contacts set firstname = one_arg where contactid = 1")
            end
</code>


{{seealso|<br/>
[[Database Selection|Performing a database selection.]]  <br/>
[[Data Object Coupling|Coupling database data and Eiffel objects.]]  <br/>
}}




