Learning to use stored procedures (Stored Procedure) is one of the necessary lessons for an ASP programmer.
all large databases support stored procedures, such as Oracle, MS SQL, and so on (but not supported by MS Access, although parameterized queries can be used in access).
stored procedures are programs that are written using the Tranact-sql language provided by SQL Server. The Tranact-sql language is the language that SQL Server provides specifically for designing database applications, which is the primary programming interface between applications and SQL Server databases. It is like the Pro-sql in an Oracle database system and the INFORMIX-4GL language available in the database system of Informix. This kind of language mainly provides the following function, lets the user design conforms to the reference request the procedure:
1), Variable description
2), ANSI-compatible SQL commands (such as select,update ...)
3), General Process Control commands (If...else ..., while ...)
4), internal function
the writing lattice for stored procedures
CREATE PROCEDURE [owner.] stored procedure name [; program number]
[Parameter # #,... Parameter #1024)]
[with
{RECOMPILE | Encryption | RECOMPILE, encryption}
]
[for REPLICATION]
as Program line
the stored procedure name cannot be more than 128 words. A maximum of 1024 parameters per stored procedure (SQL Server 7.0 version above) is used for the following parameters:
@ parameter name data type [varying] [= default] [OUTPUT]
each parameter name must have an "@" symbol, and the parameters for each stored procedure are used internally by the program only, and the type of the parameter can be used in addition to image, which is supported by other SQL Server data types.
[= default] is the same as when we set up the database to set a field defaults, here is to set the default value for this parameter. [Output] is used to specify that the parameter is both an input and an output value. That is, when the stored procedure is invoked, if the specified parameter value is the parameter we need to enter, and also needs to be output in the result, the item must be in output, and if it is only for output parameters, you can use the cursor , and you must specify both the varying and output statements when using this parameter.
CREATE PROCEDURE order_tot_amt @o_id int, @p_tot int output as
SELECT @p_tot = SUM (unitprice*quantity)
from OrderDetails
WHERE ordered= @o_id
This example is to establish a simple stored procedure Order_tot_amt, which calculates the total sales [unit Price (UnitPrice) * Quantity of the order according to the Order ID number (@o_id) entered by the user (OrderDetails) ( Quantity)], this amount is output by @p_tot This parameter to the program that invokes the stored procedure
's use of stored procedures has many advantages, it can encapsulate complex data logic, give full play to the advantages of the large database itself.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.