5. 返回值 对函数返回值的处理不同于存储过程返回值的处理,这常常导致混淆。在函数中,经常是返回一个布尔值来表明函数运行的成功与否。 If SomeFunctionName() = True Then ' Function succeeded 但在调用一个存储过程时,却不能使用同样的方法,因为存储是用Execute方法运行的,同时返回一个记录集。 Set rsAuthors = cmdAuthors.Execute 如果得不到一个返回值,如何确定是否已正确执行存储过程?当发生错误时,会报告错误,这样就可使用前一章提供的错误处理代码来处理错误。但对于一些非致命的逻辑错误怎么办? 例如,考虑向employee表添加一个新职员的情形。你可能不想防止两个职员同名的情况,但想注明这个情况。那么,可以使用一个返回值以表明是否已有同名的职员存在。存储过程如下: CREATE PROCEDURE usp_AddEmployee @Emp_ID Char(9), @FName Varchar(20), @Minit Char(1), @LName Varchar(30), @Job_ID SmallInt, @Job_Lvl TinyInt, @Pub_ID Char(4), @Hire_Date Datetime AS BEGIN DECLARE @Exists Int -- Return value
-- See if an employee with the same name exists IF EXISTS(SELECT * FROM Employee WHERE FName = @FName AND MInit = @MInit AND LName = @LName) SELECT @Exists = 1 ELSE SELECT @Exists = 0
INSERT INTO Employee (emp_id, fname, minit, lname, job_id, job_lvl, pub_id, hire_date) VALUES (@Emp_Id, @FName, @MInit, @LName, @Job_ID, @Job_Lvl, @Pub_ID, @Hire_Date) RETURN @Exists END 该过程首先检查是否有同名的职员存在,并据此设定相应的变量Exists,若存在同名,就设为1,否则为0。然后将该职员加到表中,同时把Exists的值作为返回值返回。 注意尽管返回了一个值,但并未将其声明为存储过程的参数。 调用该过程的asp代码如下: <!-- #INCLUDE FILE="../include/Connection.asp" --> <% Dim cmdEmployee Dim lngRecs Dim lngAdded
Set cmdEmployee = Server.CreateObject("ADODB.Command")
' Set the properties of the command With cmdEmployee .ActiveConnection = strConn .CommandText = "usp_AddEmployee" .CommandType = adCmdStoredProc
' Create the parameters ' Notice that the return value is the first parameter .Parameters.Append .CreateParameter ("RETURN_VALUE", adInteger, _ adParamReturnValue) .Parameters.Append .CreateParameter ("@Emp_id", adChar, adParamInput, 9) .Parameters.Append .CreateParameter ("@fname", adVarWChar, adParamInput, 20) .Parameters.Append .CreateParameter ("@minit", adChar, adParamInput, 1) .Parameters.Append .CreateParameter ("@lname", adVarWChar, adParamInput, 30) .Parameters.Append .CreateParameter ("@job_id", adSmallInt, adParamInput) .Parameters.Append .CreateParameter ("@job_lvl", adUnsignedTinyInt, adParamInput) .Parameters.Append .CreateParameter ("@pub_id", adChar, adParamInput, 4) .Parameters.Append .CreateParameter ("@hire_date", adDBTimeStamp, _ adParamInput, 8)
图9-7 按下Print Parameters按钮时显示的界面 可以简单地从这里拷贝参数行到代码中。在前面使用了一个以前从未见过的包含文件。该文件包含了几个将ADO常数(例如数据类型、参数方向等)转换为字符串值的函数: <!-- #INCLUDE FILE="../Include/Descriptions.asp" --> 接下来,定义一些变量,提取用户请求并创建Command对象。 <% Dim cmdProc Dim parP Dim strConnection Dim strProcedure Dim strQuote
' Get the connection and procedure name from the user strQuote = Chr(34) strConnection = Request.Form("txtConnection") strProcedure = Request.Form("lstProcedures")
'Update the user Response.Write "Connecting to <B>" & strConnection & "</B><BR>" Response.Write "Documenting parameters for <B>" & _ strProcedure & "</B><P><P>"
Set cmdProc = Server.CreateObject("ADODB.Command")
' Set the properties of the command, using the name ' of the procedure that the user selected With cmdProc .ActiveConnection = strConnection .CommandType = adCmdStoredProc .CommandText = strProcedure 然后使用Refresh方法自动填写Parameters集合。 .Parameters.Refresh 现在可以遍历整个集合,写出包含创建参数所需的细节内容的字符串。 For Each parP In .Parameters Response.Write ".Parameters.Append & _ "("strQuote & parP.Name & _ strQuote & ", " & _ DataTypeDesc(parP.Type) & ", " & _ ParamDirectionDesc(parP.Direction) & _ ", " & _ parP.Size & ")<BR>" Next End With
Set cmdProc = Nothing %> 在Descriptions.asp包含文件中可以找到函数DataTypeDesc和ParamDirectionDesc。 Descriptions.asp包含文件以及其他的例子文件可以在Web站点http://www.wrox.com中找到。 这是一个非常简单的技术,它较好地使用了Refresh方法。