1

如何从存储过程中获取返回值,使用VB6and SQL Server.

存储过程

ALTER PROCEDURE [dbo].[DayAttn]
    @Attn VARCHAR(12) OUTPUT
AS
BEGIN
    SELECT @Attn = MAX(id)
    FROM   table1

    RETURN @Attn
END

VB6

conn.Execute "exec DayAttn '" '" & Attn & "'"

上面的代码显示错误。

4

2 回答 2

4

此代码应向您展示如何在 VB6/ADO 经典中执行此操作:

Set cmdTemp = New ADODB.Command
Set cmdTemp.ActiveConnection = gcnxMain
cmdTemp.Parameters.Append cmdTemp.CreateParameter("Ret", adInteger, adParamReturnValue)
cmdTemp.CommandType = adCmdStoredProc
cmdTemp.CommandText = "Procname"
cmdTemp.Execute
MsgBox "Return Value: " & cmdTemp.Parameters("Ret")
于 2013-01-30T05:05:06.473 回答
1

以下是如何使用输入/输出参数:

示例存储过程

CREATE PROCEDURE [dbo].[TestRetVal]
    @P1 int,         --Parameter 1, INPUT ONLY
    @P2 int OUT      --Parameter 2, INPUT/OUTPUT
AS
BEGIN
DECLARE @RetVal int
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

SET @RetVal = @P1 + @P2  --Do whatever you need with parms

SET @P2 = 10 * @P1       --Assign value to OUTPUT parameter

RETURN @RetVal           --Assign SP return value

END

VB6 代码:

Dim cnnDB As ADODB.Connection
Dim Cmd As New ADODB.Command
Dim Param As ADODB.Parameter

'Set up your cnnDB connection...

 Cmd.CommandText = "TestRetVal"
 Cmd.CommandType = CommandTypeEnum.adCmdStoredProc

 Set Param = Cmd.CreateParameter(, adInteger, adParamReturnValue)
 Cmd.Parameters.Append Param

 Set Param = Cmd.CreateParameter("P1", adInteger, adParamInput, 8, 2)
 Cmd.Parameters.Append Param

 Set Param = Cmd.CreateParameter("P2", adInteger, adParamInputOutput, 8, 4)
 Cmd.Parameters.Append Param

 'Assuming a connection has been established
 Set Cmd.ActiveConnection = cnnDB
 Set rstTest = Cmd.Execute()

 Debug.Print Cmd.Parameters(0)       '--> 6  (Shows SP return value, value of the FIRST parameter added to cmd)
 Debug.Print Cmd.Parameters("P2")    '--> 20 (Shows current value for P2 OUT parameter)

另请参阅how-to-get-return-value-of-a-stored-procedure(感谢 Pecos Bill!)

于 2013-06-21T05:21:33.990 回答