如何使用VB6
和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 & "'"
上面的代码显示错误。
答案 0 :(得分:3)
此代码应该向您展示如何在VB6 / ADO classic中执行此操作:
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")
答案 1 :(得分:0)
以下是如何使用输入/输出参数:
示例存储过程
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!)