Results 1 to 5 of 5

Thread: stored procedure

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    320

    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

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    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:
    1. With ADOCommand
    2.    .CommandText = "sp_test"
    3.    .CommandType = adCmdStoredProc
    4.    .ActiveConnection = some ado connection
    5.    .Parameters.Append .CreateParameter("RetVal", adInteger, adParamReturnValue)
    6.    .Parameters.Append .CreateParameter("OutputVal",adVarchar,adParamOutput,10)
    7.    .Execute ,,adExecuteNoRecords
    8.  
    9.    'or Set Recordset = .Execute - if your stored procedure returns a recordset (this will be a ForwardOnly Readonly cursor).
    10.  
    11.    debug.print .Parameters("RetVal").Value, .Parameters("OutputVal").Value
    12. 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.

  3. #3
    Let me in .. techyspecy's Avatar
    Join Date
    Aug 2002
    Location
    Back to VBF.
    Posts
    2,456
    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:
    1. With ADOCommand
    2.    .CommandText = "sp_test"
    3.    .CommandType = adCmdStoredProc
    4.    .ActiveConnection = some ado connection
    5.    .Parameters.Append .CreateParameter("RetVal", adInteger, adParamReturnValue)
    6.    .Parameters.Append .CreateParameter("OutputVal",adVarchar,adParamOutput,10)
    7.    .Execute ,,adExecuteNoRecords
    8.  
    9.    'or Set Recordset = .Execute - if your stored procedure returns a recordset (this will be a ForwardOnly Readonly cursor).
    10.  
    11.    debug.print .Parameters("RetVal").Value, .Parameters("OutputVal").Value
    12. 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:
    1. Dim lAdoConn As New adodb.connection
    2.    
    3.     ' open the connection
    4.    
    5. Dim lrstAdo As New adodb.Recordset
    6.  
    7.     lAdoConn.SP_test 10, lrstAdo
    8.    
    9.     MsgBox lrstAdo.fields(0)

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Originally posted by techyspecy
    Or the simpler way ...

    VB Code:
    1. Dim lAdoConn As New adodb.connection
    2.    
    3.     ' open the connection
    4.    
    5. Dim lrstAdo As New adodb.Recordset
    6.  
    7.     lAdoConn.SP_test 10, lrstAdo
    8.    
    9.     MsgBox lrstAdo.fields(0)
    Excepting that it's an output parameter..
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    320

    **Resolved - stored procedure**

    thanks a bunch!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width