[英]Not capturing return value from stored proc in asp classic
Sorry for the long post but I wanted to include all details. 抱歉,很长的帖子,但我想提供所有详细信息。
I have a JavaScript function using ajax that calls some asp code which in turn calls a stored procedure to simply check if a record already exists. 我有一个使用ajax的JavaScript函数,该函数调用一些asp代码,然后依次调用存储过程以简单地检查记录是否已存在。 If it does exist one image appears, if it doesn't exist a different image appears. 如果确实存在,则显示一幅图像,如果不存在,则显示另一幅图像。 For some reason I am unable to capture the results in my asp variable, it returns empty, which makes the call always returns false. 由于某种原因,我无法在我的asp变量中捕获结果,它返回空值,这使调用始终返回false。 I have tested the SQL procedure by itself and it works so the data is there. 我已经单独测试了SQL过程,它可以正常工作,因此数据就在那里。 Here is the code which will explain better. 这是将更好解释的代码。
Stored Proc: 存储过程:
ALTER PROCEDURE [dbo].[UserName_Get]
-- Add the parameters for the stored procedure here
@UserName nvarchar(50)
,@FacilityID INT
,@UserNameExists INT OUT
AS
BEGIN
SELECT @UserNameExists= ISNULL((SELECT UserID FROM MyTable WHERE FacilityID = @FacilityID AND Username = @UserName),0)
END
Classic ASP code: 经典的ASP代码:
<% Response.Buffer = True %>
<!-- #include file="../../_app_init.asp" -->
<%
Dim action, p_UserName
action = SqlSanitizeStr(trim(request("action")))
'dim UserNameExists
If action = "CheckUserName" Then
' Check to see if this username already exists for this facility
' This is an Ajax routine to check if the username already exists at the current facility or not. Returns "False" if UserName does not exist.
p_UserName=SqlSanitizeStr(trim(request("UserName")))
Set objCmd = Server.CreateObject("ADODB.Command")
Set objCmd.ActiveConnection = DB_CONN
objCmd.CommandText = "UserName_Get"
objCmd.CommandType = adCmdStoredProc
objCmd.Parameters.Append objCmd.CreateParameter("@UserName", adLongVarChar, adParamInput, 50 , p_UserName)
objCmd.Parameters.Append objCmd.CreateParameter("@FacilityID", adInteger, adParamInput, , LOGIN_FACILITY_ID)
objCmd.Parameters.Append objCmd.CreateParameter("@UserNameExists", adInteger, adParamReturnValue)
objCmd.Execute ,, adExecuteNoRecords
UserNameExists = objCmd.Parameters("@UserNameExists").Value ' Get value of output parameter
If UserNameExists>0 Then
Response.Write ("True")
Else
Response.Write ("False")
End If
End If
%>
JavaScript Ajax Call JavaScript Ajax调用
<script type="text/javascript">
function CheckUserName(UserName_element) {
var UserName = UserName_element.value;
var valid_UserName_image = document.getElementById('valid_UserName_image');
var invalid_UserName_image = document.getElementById('invalid_UserName_image');
$j.ajax({
data: { action: 'CheckUserName', p_UserName: UserName },
type: 'GET',
url: 'page_logic/_check_username_ajax.asp',
success: function (result) {
//Just to see what the return value is
alert(result)
if (result == "True") {
valid_UserName_image.style.display = "none";
invalid_UserName_image.style.display = "inline";
}
else {
valid_UserName_image.style.display = "inline";
invalid_UserName_image.style.display = "none";
}
}
}
);
}
</script>
I put in alert(result) to see what was being returned, it is always empty. 我输入了alert(result)以查看返回的内容,它始终为空。
If I change the stored proc to simply 如果我将存储的过程更改为简单
SELECT @UserNameExists=0 or SELECT @UserNameExists=1
It will return a value, but of course that is hard coding the result and not actually getting records from the DB. 它将返回一个值,但是当然这是对结果进行硬编码,并且实际上没有从数据库获取记录。
The issue is the mix up between the use of OUTPUT
and RETURN
in stored procedures in SQL Server. 问题是在SQL Server的存储过程中使用OUTPUT
和RETURN
之间的混淆。
To clear things up, your stored procedure is expecting an OUTPUT
parameter instead of a RETURN
value which is something different. 为了解决问题,您的存储过程需要一个OUTPUT
参数而不是一个不同的RETURN
值。
Try changing your output parameter statement to; 尝试将输出参数语句更改为;
objCmd.Parameters.Append objCmd.CreateParameter("@UserNameExists", adInteger, adParamOutput)
For reference adParamReturnValue
is used to return the value of the RETURN
statement if used and is limited to INT
values. 作为参考, adParamReturnValue
用于返回RETURN
语句的值(如果使用的话),并且限于INT
值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.