ASP 3.0高级编程(四十一)(3)

2008-02-23 05:39:08来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折


End With
这里使用了Array函数,将单个变量转换为数组,以适于方法调用。这种方法当然也有缺点:
· 只能使用输入参数。因为不能指定参数的类型和传递方向,而缺省为输入参数。
· 如果要多次调用存储过程,这种方法速度就比较慢,因为ADO将向数据存储询问参数的内容及数据类型。
集合方法和数组方法之间在速度上的差异非常之小,几乎可以忽略。所以,如果只有输入参数,可随便使用哪一种。实际上,人们更喜欢使用Parameters集合的方法,尽管它稍为繁琐,但是使参数的属性更加明确。
4. 输出参数
我们已经知道如何获得受命令影响的记录数,如果需要更多信息,却又不想返回一个记录集,怎么办?也许想从存储过程中返回两个或三个值,但又不想费心创建一个记录集。在这时,可以定义一个输出参数,其值由存储过程提供。
例如,对于更新书价的程序,如果想在更新之后找出最高价格,可将存储过程改成:
CREATE PROCEDURE usp_UpdatePricesMax
@Type Char(12),
@Percent Money,
@Max Money OUTPUT
AS
BEGIN
UPDATE Titles
SET Price = Price * (1 @Percent / 100)
WHERE Type = @Type

SELECT @Max = MAX(Price)
FROM Titles
END
这只是在执行更新后运行了一个简单的SELECT语句,并将值赋给输出参数。
现在可以改写StroreProcedure.asp的代码从而获取变量@MAX的值。
<%
Dim cmdUpdate
Dim lngRecs
Dim strType
Dim curPercent
Dim curMax

' Get the form values
strType = Request.Form("lstTypes")
curPercent = Request.Form("txtPercent")

' Tell the user what's being done
Response.Write "Updating all books" & " of type <B>" & strType & "</B>" & _
" by " & curPercent & "%<P>"

Set cmdUpdate = Server.CreateObject("ADODB.Command")

' Set the properties of the command
With cmdUpdate
.ActiveConnection = strConn
.CommandText = "usp_UpdatePricesMax"
.CommandType = adCmdStoredProc
我们只是在集合中加入了另一个参数,但这次指定为输出参数。注意它并没有赋值,因为其值将由存储过程提供,记住这是一个输出参数。
' Add the parameters
.Parameters.Append .CreateParameter("@Type", adVarWChar, adParamInput, _
12, strType)
.Parameters.Append .CreateParameter("@Percent", adCurrency, _
adParamInput, , curPercent)
.Parameters.Append.CreateParameter("@Max", adCurrency, adParamOutput)

' Execute the command
.Execute lngRecs, , adExecuteNoRecords
一旦执行这个过程,就可从集合中取得该值。
' Extract the output parameter, which the stored
' procedure has supplied to the parameters collection
curMax = .Parameters("@Max")
End With


' And finally tell the user what's happened
Response.Write "Procedure complete. " & lngRecs & _
" records were updated.<P>"
Response.Write "The highest price book is now " & _
FormatCurrency(curMax)

Set cmdUpdate = Nothing
%>
如果有不止一个输出参数,可用相同的方法访问。可以使用参数名或索引号取出集合中的值。

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:ASP 3.0高级编程(四十)

下一篇:ASP 3.0高级编程(四十二)