First, introduce what is stored procedure
A stored procedure is a program that is 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 type of language provides the following features, allowing users to design a program that meets the needs of the reference:
1), Variable description
2), ANSI-compatible SQL commands (such as select,update ...)
3), General Process Control command (If...else ..., while ...)
4), internal function
Second, the writing of the stored procedure
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. Set up to 1024 parameters per stored procedure
(Version above SQL Server 7.0), the parameters are used as follows:
@ 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 for other SQL Server-supported 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.
Example:
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
Example Description:
The example is to establish a simple stored procedure Order_tot_amt, which calculates the total sales of the order (OrderDetails) according to the Order ID number (@o_id) entered by the user, [unit Price (UnitPrice) * Quantity ( Quantity)], this amount is output by @p_tot This parameter to the program that invokes the stored procedure
Iii. executing stored procedures in SQL Server
In SQL Server Query Analyzer, enter the following code:
DECLARE @tot_amt int
Execute Order_tot_amt 1, @tot_amt output
Select @tot_amt
The above code is to execute order_tot_amt this stored procedure, to calculate the order number 1 of the order sales amount, we define @tot_amt as the output parameter, to accept the result we want
Iv. calling stored procedures in ASP
<!--must load the Adovbs.inc file, otherwise there will be an error-->
<!--#include file= "Adovbs.inc"-->
<%
Dim objcnn
Dim objcmd
Dim Rs
Const O_ID=112
'-----Set up the Connection object----------
Set Objcnn=server.createobject ("Adodb.connection")
Objcnn.open "Driver={sql Server};server=localhost;uid=sa;pwd=cncanet;database=check;"
'-----Establish the Command object-----------
Set Objcmd=server.createobject ("Adodb.command")
Objcmd.activeconnection=objcnn
objcmd.commandtext= "Order_tot_amt" ' specifies the name of the stored procedure
Objcmd.commandtype=adcmdstoredproc ' It is stored Procedure
'-----Prepare the parameters for stored procedure-------
ObjCmd.Parameters.Append _
Objcmd.createparameter ("o_id", adinteger,adparaminput,,o_id)
ObjCmd.Parameters.Append _
Objcmd.createparameter ("P_tot", adbigint,adparamoutput,,0)
'-----Execute the stored procedure----------------------
Objcmd.execute
'-----output parameters and processing results--------------
For each parm in Objcmd.parameters
Response.Write Parm.name & "=" & Trim (parm) & "<br>"
Next
%>
The above code is passed in Win2000+iis5.0+sql Server2000