Results 1 to 4 of 4

Thread: get return value from proc

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Norwich, UK
    Posts
    405

    get return value from proc

    i haven't used ado fro a while and i've forgotten some of it. below is my ado code and the storedproc. the insert works fine. but i can't get/display the return value.

    any help would be appreciated

    Code:
        Dim cmd As New ADODB.Command
        Dim rs As New ADODB.Recordset
        Dim con As New ADODB.Connection
        Dim unidParam As ADODB.Parameter
        Dim changeReasonParam As ADODB.Parameter
        Dim changeNotesParam As ADODB.Parameter
        Dim userNameParam As ADODB.Parameter
        Dim changedToParam As ADODB.Parameter
        Dim returnValue As ADODB.Parameter
        Dim conString As String
        
        cmd.CommandType = 4
        cmd.CommandText = "sp_Save_Update"
        'set params
        Set unidParam = cmd.CreateParameter("unid", adChar, adParamInput, 32)
        Set changeReasonParam = cmd.CreateParameter("changereason", adInteger, adParamInput, 4)
        Set changeNotesParam = cmd.CreateParameter("changenotes", adVarChar, adParamInput, 2048)
        Set changedToParam = cmd.CreateParameter("changedto", adInteger, adParamInput, 4)
        Set userNameParam = cmd.CreateParameter("username", adVarChar, adParamInput, 30)
        Set returnValue = cmd.CreateParameter("returnvalue", adInteger, adParamReturnValue, 4)
        'append params to command
        cmd.Parameters.Append unidParam
        cmd.Parameters.Append changeReasonParam
        cmd.Parameters.Append changeNotesParam
        cmd.Parameters.Append changedToParam
        cmd.Parameters.Append userNameParam
        cmd.Parameters.Append returnValue
        
        unidParam.Value = UNID
        changeReasonParam.Value = ChangeReason
        changeNotesParam.Value = Notes
        changedToParam.Value = ChangedTo
        userNameParam.Value = Username
        
        '(RS) tidy this up.
        'in clsdb create a function called getcmd that returns a comand
        'object with its active connection set, then will just need to set
        'all the otehr properties
        ' Create recordset by executing the command.
        Set con = db.getConnectionObject
        conString = db.getConnectionString
        con.Open conString
        Set cmd.ActiveConnection = con
        Set rs = cmd.Execute
    Code:
    @unid char(32),
    @changereason int,
    @changenotes varchar(2048),
    @changedto int,
    @username varchar(30),
    @returnvalue int output
    
     AS
    
    insert into 
    	tbl_update(unid, username, change_reason, change_to, change_notes, date_created)
    values
    	(@unid, @username, @changereason, @changedto, @changenotes, getdate())
    
    set
    	@returnvalue = @@identity

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Norwich, UK
    Posts
    405

    Re: get return value from proc

    i have done the following, but i don't think its the best way of doing it.

    proc
    Code:
    SET NOCOUNT ON
    insert into 
    	tbl_update(unid, username, change_reason, change_to, change_notes, date_created)
    values
    	(@unid, @username, @changereason, @changedto, @changenotes, getdate())
    
    	select @@identity as 'identity'
    vb
    Code:
    rs.Fields("identity").Value

  3. #3
    Fanatic Member Bombdrop's Avatar
    Join Date
    Apr 2001
    Location
    St Helens, England, UK
    Posts
    667

    Re: get return value from proc

    herse the way i do it.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4. Dim conPolicyMaster As ADODB.Connection
    5. Dim cmdPolicyMaster As ADODB.Command
    6. Dim recPolicyMaster As ADODB.Recordset
    7. Dim Params As ADODB.Parameters
    8. Dim Param As ADODB.Parameter
    9.  
    10. Set conPolicyMaster = New ADODB.Connection
    11. Set cmdPolicyMaster = New ADODB.Command
    12.  
    13. Dim strConnString As String
    14. strConnString = Connection string
    15.  
    16. 'Set connection properties and open
    17. conPolicyMaster.ConnectionString = strConnString
    18. conPolicyMaster.CursorLocation = adUseClient
    19. conPolicyMaster.Open
    20.  
    21. 'Set command properties
    22. Set cmdPolicyMaster.ActiveConnection = conPolicyMaster
    23. cmdPolicyMaster.CommandText = "testh1"
    24. cmdPolicyMaster.CommandType = adCmdStoredProc
    25. Set Params = cmdPolicyMaster.Parameters
    26.  
    27. 'Define stored procedure parameters and append to command.
    28. Params.Append cmdPolicyMaster.CreateParameter("RETURN_VALUE", adInteger, adParamReturnValue, 0)
    29. Params.Append cmdPolicyMaster.CreateParameter("@clientshort", adVarChar, adParamInput, 10)
    30.  
    31. 'Specify input parameters
    32. Params("@clientshort") = "TEST"
    33.  
    34. 'Execute the command
    35. Set recPolicyMaster = cmdPolicyMaster.Execute
    36.  
    37. 'Disconnect recordset
    38. Set recPolicyMaster.ActiveConnection = Nothing
    39.  
    40. 'Close connection
    41. conPolicyMaster.Close
    42.  
    43. 'YOU can access the reutrn using either of these methods
    44. Debug.Print cmdPolicyMaster.Parameters(0).Value
    45. Debug.Print cmdPolicyMaster.Parameters("RETURN_VALUE")
    46. End Sub

    Hope this helps!!

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: get return value from proc

    The SET NOCOUNT ON is kind of required in all SPROCS - as the ERROR COLLECTION of the COMMAND object gets populated with all those ROWS AFFECTED values if you don't use SET NOCOUNT ON.

    You will notice that BombDrop closes the recordset before attempting to view the return parameters. That's meaningful, because ADO will not expose those parameters while there are still RECORDSET and ERROR collections to process from the server to the client.

    Also, if the parameter is in the argument list it's not really a RETURN PARAMETER, really an output parameter...

    We have several in our SPROCS

    Code:
        With objCmd
            .CommandTimeout = 0
            .CommandText = s1
            .ActiveConnection = gCn
            .CommandType = adCmdStoredProc
            
            .Parameters.Append .CreateParameter("@PassConnId", adInteger, adParamInput, 8, glngConnId)
            .Parameters.Append .CreateParameter("@RetStat", adInteger, adParamOutput, 8)
            .Parameters.Append .CreateParameter("@RetText", adVarChar, adParamOutput, 100)
            .Parameters.Append .CreateParameter("@RetMode", adInteger, adParamOutput, 8)
            .Parameters.Append .CreateParameter("@RetGrid", adInteger, adParamOutput, 8)
            .Parameters.Append .CreateParameter("@RetExtra", adVarChar, adParamOutput, 100)

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

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