Results 1 to 12 of 12

Thread: [RESOLVED] Collect a returned value from SP

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2005
    Posts
    31

    Resolved [RESOLVED] Collect a returned value from SP

    Hi all

    I have a Stored Procedure (SQL server 2000) that returns a Integer Value. i am calling the Sp from VB.Net code. How Do i collect the returned value.

    Any suggestion!!

    Thanks.

  2. #2
    Hyperactive Member eranfox's Avatar
    Join Date
    May 2001
    Posts
    492

    Re: Collect a returned value from SP

    Hello goluguddu,
    I don't know DOT NET yet but i know how to do this in vb6.
    There is a small example in SQLServer books on-line:
    search under ADO and SQL Server the title:
    "Using Return Code and Output Parameters for Stored Procedures"

    but here is the code anyway:
    The following stored procedure contains one input parameter, one output parameter, and a return parameter. The procedure selects those rows in the titles table of the pubs database where the royalty percent paid to the author is greater than the amount entered by the user (the input parameter). The program returns the number of rows as the output variable. If the program returns any rows, a return code of 0 is issued; if no rows are returned, a return code of 99 is issued.

    Code:
    USE pubs
    GO
    CREATE PROCEDURE myProc
    @outparm      int      OUTPUT
    @inparm      int
    AS
    SELECT * FROM titles WHERE royalty > @inparm
    SELECT @outparm = COUNT (*) FROM TITLES WHERE royalty > @inparm
    IF (@outparm > 0)
    RETURN 0
    ELSE
    RETURN 99
    GO
    An ADO code program that executes the stored procedure myProc is shown here.
    VB Code:
    1. Dim cn As New ADODB.Connection
    2. Dim cmd As New ADODB.Command
    3. Dim rs As New ADODB.Recordset
    4. Dim fldloop As ADODB.Field
    5. Dim param1 As Parameter, param2 As Parameter, param3 As Parameter
    6. Dim provStr As String
    7. Dim royalty As Variant
    8.    
    9. Private Sub spStart()
    10.  
    11. ' Connect using the SQLOLEDB provider.
    12. cn.Provider = "sqloledb"
    13.  
    14. ' Specify connection string on Open method.
    15. provStr = "Server=MyServer;Database=pubs;Trusted_Connection=yes"
    16. cn.Open provStr
    17.  
    18. ' Set up a command object for the stored procedure.
    19. Set cmd.ActiveConnection = cn
    20. cmd.CommandText = "myProc"
    21. cmd.CommandType = adCmdStoredProc
    22.  
    23. ' Set up a return parameter.
    24. Set param1 = cmd.CreateParameter("Return", adInteger, adParamReturnValue)
    25. cmd.Parameters.Append param1
    26.            
    27. ' Set up an output parameter.
    28. Set param2 = cmd.CreateParameter("Output", adInteger, adParamOutput)
    29. cmd.Parameters.Append param2
    30.  
    31. ' Set up an input parameter.
    32. Set param3 = cmd.CreateParameter("Input", adInteger, adParamInput)
    33. cmd.Parameters.Append param3
    34. royalty = Trim(InputBox("Enter royalty:"))
    35. param3.Value = royalty
    36.  
    37. ' Execute command, and loop through recordset, printing out rows.
    38. Set rs = cmd.Execute
    39.  
    40. Dim i As Integer
    41. While Not rs.EOF
    42.     For Each fldloop In rs.Fields
    43.         Debug.Print rs.Fields(i)
    44.         i = i + 1
    45.     Next fldloop
    46.     Debug.Print ""
    47.     i = 0
    48.     rs.MoveNext
    49. Wend
    50.  
    51. ' Need to close recordset before getting return
    52. ' and output parameters.
    53. rs.Close
    54.  
    55. Debug.Print "Program ended with return code: " & Cmd(0)
    56. Debug.Print "Total rows satisfying condition: " & Cmd(1)
    57. cn.Close
    58.  
    59. End Sub
    Best Regards,
    ERAN
    Eran Fox
    ASSEMBLER,C,C++,VB6,SQL...

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Collect a returned value from SP

    For ADO.NET that code is useless. If you are using an output parameter, and not also returning a recordset, create the connectioon, the command, add the parameters, then use .ExecuteNoQuery. After than, check the proper parameter, and the value should be there.

    If you want to capture it ala a RETURN from SQL, there are two ways. 1 - use the same parameter method above, BUT the return parameter MUST be the first parameter added, and it has to be of direction type Return Value. The second assumes no putput parameters and no recordset is returned either. And that's to use the .ExecuteScalar function.

    -tg
    * 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??? *

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Sep 2005
    Posts
    31

    Re: Collect a returned value from SP

    Quote Originally Posted by techgnome
    For ADO.NET that code is useless. If you are using an output parameter, and not also returning a recordset, create the connectioon, the command, add the parameters, then use .ExecuteNoQuery. After than, check the proper parameter, and the value should be there.

    If you want to capture it ala a RETURN from SQL, there are two ways. 1 - use the same parameter method above, BUT the return parameter MUST be the first parameter added, and it has to be of direction type Return Value. The second assumes no putput parameters and no recordset is returned either. And that's to use the .ExecuteScalar function.

    -tg
    Hi Thanks for you reply. I dont have output variable in the Sp. Please suggest me the changes in the following code.

    VB Code:
    1. CREATE PROCEDURE [dbo].[ins_Template]
    2. @vchar_TemplateName varchar(100),       -- Template Name
    3. @int_CustomerId int,            -- Customer Id
    4.  
    5. -- INSERT
    6. INSERT INTO dbo.map_Templates (TemplateName, CustomerId)
    7. VALUES  (@vchar_TemplateName, @int_CustomerId)
    8.  
    9. SET @int_TemplateId = @@IDENTITY
    10.  
    11. RETURN @int_TemplateId
    12. GO
    13.  
    14.  
    15. SQL = "EXEC ins_Template " & _
    16.                       "@vchar_TemplateName = '" & Name & "'," & _
    17.                       "@int_CustomerId = " & CustomerID
    18.  
    19. Dim UCommand As SqlClient.SqlCommand = New SqlCommand(SQL, BlitzKarte4.GlobalConnection)
    20.  
    21. UCommand.ExecuteNonQuery()

    Thanks.

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Collect a returned value from SP

    Change

    UCommand.ExecuteNonQuery()

    to


    RetCode = UCommand.ExecuteScalar()

    Where RetCode is your variable where you want top store the return value.

    -tg
    * 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??? *

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Sep 2005
    Posts
    31

    Re: Collect a returned value from SP

    It is not working. The return value I get is not correct.

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Collect a returned value from SP

    And that's because I missed how you were constructing your SQL....

    VB Code:
    1. UCommand.Parameters.Add(New SqlClient.SqlParameter("@vchar_TemplateName", SqlDbType.VarChar, 100))
    2.         UCommand.Parameters.Add(New SqlClient.SqlParameter("@int_CustomerId", SqlDbType.Int))
    3.  
    4.         UCommand.Parameters("@vchar_TemplateName").Value = Name
    5.         UCommand.Parameters("@int_CustomerId").Value = CustomerID
    6.  
    7.         MyResult = UCommand.ExecuteScalar()

    -tg
    * 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??? *

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Sep 2005
    Posts
    31

    Re: Collect a returned value from SP

    Hi thanks alot for help..but still its not working. Here is the code I am running....

    VB Code:
    1. SQL = "EXEC ins_Template " & _
    2.                       "@vchar_TemplateName = '" & Name & "'," & _
    3.                       "@int_CustomerId = " & CustomerID
    4.  
    5. Dim UCommand As SqlClient.SqlCommand = New SqlCommand(SQL, BlitzKarte4.GlobalConnection)
    6.  
    7. UCommand.Parameters.Add(New SqlClient.SqlParameter("@vchar_TemplateName", SqlDbType.VarChar, 100))
    8.         UCommand.Parameters.Add(New SqlClient.SqlParameter("@int_CustomerId", SqlDbType.Int))
    9.  
    10.         UCommand.Parameters("@vchar_TemplateName").Value = Name
    11.         UCommand.Parameters("@int_CustomerId").Value = CustomerID
    12.  
    13.         MyResult = UCommand.ExecuteScalar()

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Collect a returned value from SP

    Garrgh.... (that was directed at myself, I left off some info)....

    I meant to preface that with this:
    Don't use that for your SQL. Your CommandText should be just the name of the stored procedure and nothing else. Also be sure to set the CommandType to StoredProcedure before you execute it (idealy right after you set the command text)

    -tg
    * 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??? *

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Sep 2005
    Posts
    31

    Re: Collect a returned value from SP

    well...Its not working(i=0).....Dont I need to add returned parameter "@int_TemplateId" to the UCommand as well.

    The code is....

    VB Code:
    1. Dim i as integer
    2. UCommand.CommandText = "ins_Template"
    3. UCommand.CommandType = CommandType.StoredProcedure
    4. UCommand.Connection = BlitzKarte.GlobalConnection
    5.  
    6. UCommand.Parameters.Add(New SqlClient.SqlParameter("@vchar_TemplateName", SqlDbType.VarChar, 100))
    7. UCommand.Parameters.Add(New SqlClient.SqlParameter("@int_CustomerId", SqlDbType.Int))
    8.  
    9. UCommand.Parameters("@vchar_TemplateName").Value = Name
    10. UCommand.Parameters("@int_CustomerId").Value = CustomerID  
    11.  
    12. i = UCommand.ExecuteScalar()
    13. UCommand.Dispose()

  11. #11
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Collect a returned value from SP

    no, because of this line in the SP: RETURN @int_TemplateId

    It returns the int_TemplateID tthrough the sP.... try thids....

    VB Code:
    1. Dim i as integer
    2. UCommand.CommandText = "ins_Template"
    3. UCommand.CommandType = CommandType.StoredProcedure
    4. UCommand.Connection = BlitzKarte.GlobalConnection
    5.  
    6. UCommand.Parameters.Add(New SqlClient.SqlParameter("@int_TemplateID", SqlDbType.VarChar, 100))
    7. UCommand.Parameters.Add(New SqlClient.SqlParameter("@vchar_TemplateName", SqlDbType.VarChar, 100))
    8. UCommand.Parameters.Add(New SqlClient.SqlParameter("@int_CustomerId", SqlDbType.Int))
    9.  
    10. UCommand.Parameters("@int_TemplateID").Direction =ParameterDirection.ReturnValue
    11. UCommand.Parameters("@vchar_TemplateName").Value = Name
    12. UCommand.Parameters("@int_CustomerId").Value = CustomerID  
    13.  
    14. UCommand.ExecuteNonQuery()
    15. i= UCommand.Parameters("@int_TemplateID").Value
    16.  
    17. UCommand.Dispose()

    Then change your SP like this:

    Code:
    CREATE PROCEDURE [dbo].[ins_Template]
    @vchar_TemplateName varchar(100),		-- Template Name
    @int_CustomerId int,			-- Customer Id
    
    -- INSERT
    INSERT INTO dbo.map_Templates (TemplateName, CustomerId)
    VALUES	(@vchar_TemplateName, @int_CustomerId)
    
    SET @int_TemplateId = @@IDENTITY
    
    RETURN @int_TemplateId
    GO

    -tg
    * 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??? *

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Sep 2005
    Posts
    31

    Re: Collect a returned value from SP

    Awesom....It works. Thanks a lot.
    Great job!!!

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