通过ASP(Classic VBSCRIPT)从SQL存储过程检索OUTPUT变量时出现问题

时间:2011-02-14 21:10:46

标签: sql-server-2005 vbscript asp-classic

我是新来的,这是我的第一篇文章。我在SQL Server中创建了一个存储过程,它有(4)输入参数和(1)输出参数。存储过程用于在表中插入记录或从表中删除记录。为输出参数分配一条消息以返回到ASP页面,以提供有关存储过程中发生的事件的反馈。即。 “记录插入”,“记录已存在”或“记录已删除”。

当我从网页运行代码时,会添加和删除记录。我似乎在获取输出参数返回值时遇到的唯一问题。

当我从SQL Server Management Studio执行存储过程时,将显示输出参数。

我搜索了互联网,并根据其他示例检查了我的代码。我不知道为什么我不会得到返回的值。任何帮助将不胜感激。

CREATE PROC My_Stored_Procedure
    -- Add the parameters for the stored procedure here

    @ContactID AS INT = '0', /* Contact ID relates to br_UID from the dbo.Reporting_Contacts table */
    @FacNum AS CHAR(4) = '0866', /* Facility Number from the Reporting Admin Page on Intranet */
    @EmployeeID AS CHAR(8) = '10023258', /* EmployeeID from the Reporting Admin Page on Intranet */
    @Delete AS CHAR(1) = 'N', /* Delete flag value to be passed from webpage on Intranet */
    @OutputMessage AS NVARCHAR(200) = 'test' OUTPUT /* To send back sucuess or error messages */

AS

BEGIN

    -- SET NOCOUNT ON added to prevent extra result sets from

    -- interfering with SELECT statements.

    SET NOCOUNT ON;

    -- Set variables for use in Stored Procedure


    -- Insert statements for procedure here

IF  @Delete = 'N'

        BEGIN


IF @EmployeeID  NOT IN (SELECT HGISSN FROM Main)

BEGIN

Select * FROM dbo.Reporting_Contacts

SET @OutputMessage = 'Contact does not exist as an employee.'

END


IF @EmployeeID IN (SELECT employeeID FROM dbo.Reporting_Contacts WHERE EmployeeID = @EmployeeID AND FacilityNumber = @FacNum)


BEGIN

SELECT @OutputMessage = 2

PRINT @OutputMessage 

SET @OutputMessage =  'This Person is already a contact.'


END

IF @EmployeeID IN (SELECT HGISSN FROM dbo.EMP_Table) AND @EmployeeID NOT IN (SELECT employeeID FROM dbo.Reporting_Contacts WHERE EmployeeID = @EmployeeID AND FacilityNumber = @FacNum)

BEGIN

INSERT INTO dbo.Reporting_Contacts (employeeID, FacilityNumber, ContactLevel)

VALUES (@EmployeeID, @FacNum, 'S')

SET @OutputMessage = 'New Contact has been added.'

END

END


ELSE IF @Delete = 'Y'

        BEGIN


DELETE FROM dbo.Reporting_Contacts WHERE br_UID = @ContactID


SET @OutputMessage = 'Record Deleted'


END



END

SELECT @OutputMessage

GO

这是我的VBScript:

Dim addContacts__ContactID

addContacts__ContactID = "0"

if(Request("ContactID") <> "") then addContacts__ContactID = Request("ContactID")



Dim addContacts__FacNum

addContacts__FacNum = LEFT(Request.Form("FacilityNumber"),4)

if(Request("FacNum") <> "") then addContacts__FacNum = Request("FacNum")



Dim addContacts__EmployeeID

addContacts__EmployeeID = Request.Form("EmployeeID")

if(Request("EmployeeID") <> "") then addContacts__EmployeeID = Request("EmployeeID")



Dim addContacts__Delete

addContacts__Delete = "N"

if(Request("Delete") <> "") then addContacts__Delete = Request("Delete")




Dim strReturnMessage


set addContacts = Server.CreateObject("ADODB.Command")

addContacts.ActiveConnection = MY_STRING

addContacts.CommandText = "My_Stored_Procedure"

addContacts.CommandType = 4 'Evaluate as a stored procedure

addContacts.CommandTimeout = 0

addContacts.Prepared = true

addContacts.Parameters.Append addContacts.CreateParameter("@RETURN_VALUE", 3, 4)

addContacts.Parameters.Append addContacts.CreateParameter("@ContactID", 3, 1,10,addContacts__ContactID)

addContacts.Parameters.Append addContacts.CreateParameter("@FacNum", 129, 1,4,addContacts__FacNum)

addContacts.Parameters.Append addContacts.CreateParameter("@EmployeeID", 129, 1,8,addContacts__EmployeeID)

addContacts.Parameters.Append addContacts.CreateParameter("@Delete", 129, 1,1,addContacts__Delete)

addContacts.Parameters.Append addContacts.CreateParameter("@OutputMessage", 200, 2,200)


strReturnMessage =  addContacts.Parameters("@OutputMessage")

2 个答案:

答案 0 :(得分:2)

尝试这样的事情

Function LoginUser()
    Dim cmd
    Set cmd = Server.CreateObject("ADODB.Command")
    Set cmd.ActiveConnection = dbconn
    With cmd
        .CommandText = "sproc_login"
        .Parameters.Append  .CreateParameter("@RETURN_VALUE", adInteger, adParamReturnValue)
        .Parameters.Append  .CreateParameter("@username", adVarChar, adParamInput,50,username)
        .Parameters.Append  .CreateParameter("@password", adVarChar, adParamInput,50,password)
        .execute
        strResponse = CInt(.Parameters(0).value )
    End With
    With Response
        If strResponse > 0 Then
            .Write strSuccess
        Else
            .Write strFailure
        End If
    End With
    Set cmd = Nothing
End Function

++ 示例仅使用需要修改 ++

答案 1 :(得分:1)

您使用相同的变量作为整数和字符串。 存储过程定义了一个OUTPUT参数 - @OutputMessage。该变量由存储过程修改并返回。这是你应该捕捉的变量。 存储过程中的代码很混乱。你想@OutputMessage是一个字符串还是一个整数? 例如,请参阅SP中的这些相邻行:

SELECT @OutputMessage = 2

PRINT @OutputMessage  

SET @OutputMessage =  'This Person is already a contact.'

第一行是将@OutputMessage视为一个整数(当SET语句就足够时使用SELECT语句是什么?) 然后你打印,没有投诉。 然后将其设置为文本值(至少这次使用SET语句)。

回顾SP的定义,并将参数定义为: @OutputMessage AS NVARCHAR(200)='test'OUTPUT 它不是整数。

当您尝试使用cint转换结果时,VB脚本似乎期望一个整数,但SP中变量​​的默认值是“test”。

With cmd
    .NamedParameters = true 'Add this line to make things easier to read
    .CommandText = "sproc_login" 
    '.Parameters.Append  .CreateParameter("@RETURN_VALUE", adInteger, adParamReturnValue)  'This is useless and should be removed.
    .Parameters.Append  .CreateParameter("@username", adVarChar, adParamInput,50,username) 
    .Parameters.Append  .CreateParameter("@password", adVarChar, adParamInput,50,password) 

    .Parameters.Append (cmd.CreateParameter ("@OutputMessage",adInteger,adParamOutput)) 'This is the variable being returned so get it.
    .execute 
    'strResponse = CInt(.Parameters("@Output").value ) 'Confused - @OutputMessage is defined as NVARCHAR(200) - not INTEGER.
strResponse = .Parameters("@OutputMessage").value 'This is much better.
End With 

“响应”区域中的代码也很混乱,应该重写。

通过使用整数和字符串清除混淆,代码可以正常工作。