|
-
Apr 2nd, 2003, 03:08 PM
#1
Thread Starter
Hyperactive Member
stored procedure
I have a test stored procedure in sql server. For eg
CREATE PROCEDURE SP_test @sum varchar(10) output
As
BEGIN
select @sum = '1000'
end
go
Please let me know the syntax, how do I get its return
value using ADO command object in VB6
-
Apr 2nd, 2003, 03:34 PM
#2
Don't confuse Return Value with Output Values. They are not the same and handled somewhat differently.
A Return Value can only be an integer and must be explicitly set in the stored procedure. Use the Return statement ie, Return 999 (or Return @RetValue when using a local variable).
To get the Return value or Output Value you need to add parameters to the ADO command object. Note - the Return Value parameter must be the first one appended to the collection ( unless you are using NamedParameters) but it can be omitted, if your application does not care about its value.
Here is a sample for your test procedure.
VB Code:
With ADOCommand
.CommandText = "sp_test"
.CommandType = adCmdStoredProc
.ActiveConnection = some ado connection
.Parameters.Append .CreateParameter("RetVal", adInteger, adParamReturnValue)
.Parameters.Append .CreateParameter("OutputVal",adVarchar,adParamOutput,10)
.Execute ,,adExecuteNoRecords
'or Set Recordset = .Execute - if your stored procedure returns a recordset (this will be a ForwardOnly Readonly cursor).
debug.print .Parameters("RetVal").Value, .Parameters("OutputVal").Value
End With
If you are unsure, you can have ADO populate the parameters for you by doing a .Parameters.Refresh just after the .ActiveConnection line above (and of course omit the parameters.Append lines). Then using the Locals window or debug.print you can see what you need to include for each parameter.
-
Apr 2nd, 2003, 03:57 PM
#3
Let me in ..
Originally posted by brucevde
Don't confuse Return Value with Output Values. They are not the same and handled somewhat differently.
A Return Value can only be an integer and must be explicitly set in the stored procedure. Use the Return statement ie, Return 999 (or Return @RetValue when using a local variable).
To get the Return value or Output Value you need to add parameters to the ADO command object. Note - the Return Value parameter must be the first one appended to the collection ( unless you are using NamedParameters) but it can be omitted, if your application does not care about its value.
Here is a sample for your test procedure.
VB Code:
With ADOCommand
.CommandText = "sp_test"
.CommandType = adCmdStoredProc
.ActiveConnection = some ado connection
.Parameters.Append .CreateParameter("RetVal", adInteger, adParamReturnValue)
.Parameters.Append .CreateParameter("OutputVal",adVarchar,adParamOutput,10)
.Execute ,,adExecuteNoRecords
'or Set Recordset = .Execute - if your stored procedure returns a recordset (this will be a ForwardOnly Readonly cursor).
debug.print .Parameters("RetVal").Value, .Parameters("OutputVal").Value
End With
If you are unsure, you can have ADO populate the parameters for you by doing a .Parameters.Refresh just after the .ActiveConnection line above (and of course omit the parameters.Append lines). Then using the Locals window or debug.print you can see what you need to include for each parameter.
Or the simpler way ...
VB Code:
Dim lAdoConn As New adodb.connection
' open the connection
Dim lrstAdo As New adodb.Recordset
lAdoConn.SP_test 10, lrstAdo
MsgBox lrstAdo.fields(0)
-
Apr 2nd, 2003, 04:06 PM
#4
Originally posted by techyspecy
Or the simpler way ...
VB Code:
Dim lAdoConn As New adodb.connection
' open the connection
Dim lrstAdo As New adodb.Recordset
lAdoConn.SP_test 10, lrstAdo
MsgBox lrstAdo.fields(0)
Excepting that it's an output parameter..
-
Apr 3rd, 2003, 07:40 AM
#5
Thread Starter
Hyperactive Member
**Resolved - stored procedure**
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|