13. Stored procedure/command/callable
Many advanced Web
applications use a stored procedure application object. Stored
procedures are known under a variety of names: ASP users call them commands, whereas JSP developers know them as callables;
only ColdFusion users refer to them solely as stored procedures. Stored
procedures are complete SQL queries that may return recordsets or other
data. Stored procedures are often used for complex data source
management such as inserting new tables on-the-fly.
Stored procedures are
created and compiled in the data source itself, such as Microsoft's SQL
Server. Because they are precompiled, they execute faster than similar
SQL statements entered directly into the Web application. Stored
procedures, like recordsets, can be defined as a data source through
either the Bindings panel or the Server Behaviors panel.
To define a stored procedure as a data source through the Server Behaviors panel, follow these steps:
From
the Server Behaviors panel, click the Add (+) button and, depending on
your server model: for ASP, choose Command (Stored Procedure); for
ColdFusion, choose Stored Procedure. The stored procedure dialog box for
the appropriate server model is displayed; for example, Figure 20 shows the Command dialog box seen by ASP users.
NOTE
Stored procedures and the
variables necessary to use them are database- and server
model–dependent. A stored procedure on SQL server is completely
different from a ColdFusion stored procedure or another SQL-type
database. The Stored Procedure/Command/Callable dialog box may change
based on the type of database to which you're connecting, as well as the
server model.
In the Command/Callable/Stored Procedure field, enter a unique name.
From the Connection list, choose the connection in which this stored procedure may be found.
ASP users should choose Stored Procedure from the Type list.
NOTE
The ASP Command (Stored
Procedure) server behavior includes additional types: Insert, Update,
and Delete. These work identically to the Prepared
(Insert/Update/Delete) server behavior.
If
the stored procedure returns a recordset, choose the Return Recordset
option and enter a name in the Returned Recordset Named field.
From
the Database Items area, expand the Stored Procedures list and choose
the desired stored procedure. It's a good idea to click Test to be sure
your connection is working properly at this point.
To modify the stored procedure, select any other element in the Database Items area and click the Procedure or Where button.
Enter
any necessary variables by clicking the Add (+) button in the Variables
area and entering the values under each column: Name, Type, Direction,
Size, Default Value, and Runtime Value.
Click OK when you're finished.