Results 1 to 12 of 12

Thread: [RESOLVED] VB & SQLStoredProcedures

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2004
    Location
    Mumbai
    Posts
    236

    Resolved [RESOLVED] VB & SQLStoredProcedures

    hi,

    i am using vb and storedprocedures., in vb adodb.command is used. i am inserting into a table using stored procedure. after inserting i want to return the inserted row to a recordset.

    in a select storedprocedure the "set rs = command1.execute" works fine. but in a insert sp, after the insert statement any select statement is not considered. so in insert sp, "set rs = command1.execute" does not work. can anyone explain me why and is there any way to return the row in the insert sp itself.
    --Kishore...

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Jul 2004
    Location
    Mumbai
    Posts
    236

    Re: VB & SQLStoredProcedures

    hi again

    it is possible in vb's adodb.command, to use the output parameter type or returnparameter type and return single value from storedprocedure. but how to return a recordset itself, like in any select_storedprocedure..
    --Kishore...

  3. #3
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: VB & SQLStoredProcedures

    Could you post your code in your StoredProc?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jul 2004
    Location
    Mumbai
    Posts
    236

    Re: VB & SQLStoredProcedures

    hi,

    Code is here
    VB Code:
    1. CREATE PROCEDURE [sInsert_mPEmp]
    2.     ( @E_Fname_2    [nvarchar](255),
    3.      @E_Mname_3     [nvarchar](255),
    4.      @E_Lname_4     [nvarchar](255),
    5.      @E_Desg_5  [int],
    6.      @E_DOB_6   [smalldatetime],
    7.      @E_DOJ_7   [smalldatetime],
    8.      @E_LDR_8   [smalldatetime],
    9.      @E_Status_9    [int])
    10.  
    11. AS
    12. begin
    13.  INSERT INTO [DBP].[dbo].[mPEmp]
    14.      ([E_Fname],
    15.      [E_Mname],
    16.      [E_Lname],
    17.      [E_Desg],
    18.      [E_DOB],
    19.      [E_DOJ],
    20.      [E_LDR],
    21.      [E_Status])
    22.  
    23. VALUES
    24.     ( @E_Fname_2,
    25.      @E_Mname_3,
    26.      @E_Lname_4,
    27.      @E_Desg_5,
    28.      @E_DOB_6,
    29.      @E_DOJ_7,
    30.      @E_LDR_8,
    31.      @E_Status_9)
    32.  
    33. select E_Id, E_Fname + ' ' + E_MName + ' ' + E_LName as E_Name, E_Desg from mpEmp where E_Id = (select max(E_Id) from mpemp)
    34. end
    35. GO

    if I remove then Insert part and keep the select query then it works fine. the sp works fine in QueryAnalyser(it inserts and it returns the inserted row.). so i think the problem is in Adodb.command. [i guess it will not accept returned rowset, when insert statement is used in sp.]
    --Kishore...

  5. #5
    Addicted Member
    Join Date
    Jun 2005
    Posts
    139

    Re: VB & SQLStoredProcedures

    I've tried with a similar sp and it works file
    VB Code:
    1. Dim myCon As New ADODB.Connection
    2.    Dim myCmd As New ADODB.Command
    3.    Dim rs As Recordset
    4.    
    5.    myCon.Open myConnectionString
    6.    myCmd.ActiveConnection = con
    7.    myCmd.CommandText = "sp_mysp"
    8.    myCmd.CommandType = adCmdStoredProc
    9.    myCmd.Parameters.Append myCmd.CreateParameter("param1", adVarChar, adParamInput, 10, "ABCD")
    10.    
    11.    Set rs = myCmd.Execute

    Another option is
    VB Code:
    1. Dim myCon As New ADODB.Connection
    2.    Dim rs As Recordset
    3.  
    4.    myCon.Open myConnectionString
    5.    
    6.    rs.open "exec sp_mysp",myCon,adOpenStatic,adLockReadOnly

    In both the above examples, the recordset retrieves the inserted rows

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jul 2004
    Location
    Mumbai
    Posts
    236

    Re: VB & SQLStoredProcedures

    hi,

    the code when uses select SP then it works fine. or if Insert alone is used then also works fine. but when it the sp executes a Insert command and then followed by select., the result of select is not received by ADODb.command. i say adodb.command does not receives bcos when i run in sql query analyser, the insert and select both get executed and the select displays the result..

    regarding the second option you want me to execute the sp like
    VB Code:
    1. rs.open "exec insert_sp " & param1 & "," & param2 & "," ...,myCon,adOpenStatic,adLockReadOnly

    Hmmmm, it look tedious with 10 or 15 parameters, anyhow i'll try it. i am trying out other options also. thank you for the response..
    --Kishore...

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

    Re: VB & SQLStoredProcedures

    Our entire application is build upon this stuff and it works just fine. I suspect there's something else wrong here. The SP looks OK, so let's look at the code.

    A few questions:
    How do you know that it's not working?
    What does your VB code look like?

    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
    Addicted Member
    Join Date
    Jul 2004
    Location
    Mumbai
    Posts
    236

    Re: VB & SQLStoredProcedures

    hi,

    i say that adodb.command is not working bcos when i execute the sp in queryanalyser it works fine and returns the inserted row., but it does'nt even open the recordset in adodb.command(means rs.state is 0).


    VB Code:
    1. com.CommandType = adCmdStoredProc
    2.   com.CommandText = "sInsert_mpEmp"
    3.   Set par = com.CreateParameter("Fname", adVarChar, adParamInput, 100, txtFName.Text)
    4.   com.Parameters.Append par 'com.Parameters("@E_Fname_2") = par
    5.   Set par = com.CreateParameter("Mname", adVarChar, adParamInput, 100, txtMName.Text)
    6.   com.Parameters.Append par 'com.Parameters("@E_Mname_3") = par
    7.   Set par = com.CreateParameter("Lname", adVarChar, adParamInput, 100, txtLName.Text)
    8.   com.Parameters.Append par 'com.Parameters("@E_Lname_4") = par '
    9.   Set par = com.CreateParameter("Designation", adInteger, adParamInput, , cmbDesignation.List(cmbDesignation.ListIndex, 1))
    10.   com.Parameters.Append par 'com.Parameters("@E_Desg_5") = par '
    11.   Set par = com.CreateParameter("DOB", adDate, adParamInput, , dtDOB.Value)
    12.   com.Parameters.Append par 'com.Parameters("@E_DOB_6") = par '
    13.   Set par = com.CreateParameter("DOJ", adDate, adParamInput, , dtDOJ.Value)
    14.   com.Parameters.Append par 'com.Parameters("@E_DOJ_7") = par '
    15.   Set par = com.CreateParameter("LDOR", adDate, adParamInput, , dtLDOR.Value)
    16.   com.Parameters.Append par 'com.Parameters("@E_LDR_8") = par '
    17.   Set par = com.CreateParameter("Status", adInteger, adParamInput, , cmbEStatus.List(cmbEStatus.ListIndex, 1))
    18.   com.Parameters.Append par 'com.Parameters("@E_Status_9") = par '
    19.   set rs = com.Execute
    --Kishore...

  9. #9
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: VB & SQLStoredProcedures

    Every SQL statement (insert, update, select, deletes) executed in a stored procedure return a result to the client. The only way ADO can capture these results is via its Recordset object.

    The Recordset is closed because the Insert statement does not return records. However, there is another recordset available via the Recordset.NextRecordset property

    Two options to solve this problem.

    set rs = com.Execute ' first recordset contains result of Insert statement.
    set rs = rs.nextRecordset 'next recordset contains result of Select statement

    or

    Add Set NoCount On to the beginning of your stored procedure. This means only the result of the last sql statement, i.e. your Select, will return its results to ADO. There would be no need to use rs.NextRecordset.

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

    Re: VB & SQLStoredProcedures

    I agree with BRUCEVDE - use the SET NOCOUNT ON - it will eliminate the unwanted "rows affected" block from coming back to the client after the INSERT - which is getting in the way of you seeing the SELECT recordset.

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

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

    Re: VB & SQLStoredProcedures

    We've gotten into the habit of settin RowCount OFF that I forgot about that. Something else we normaly do is that an insert is supposed to do just that... insert. We typicaly use an outbound param to return the id, then make a separate trip back to retrieve the record.... and unless we have good reason to do otherwise, that comes back as output parameters too.....

    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
    Addicted Member
    Join Date
    Jul 2004
    Location
    Mumbai
    Posts
    236

    Re: VB & SQLStoredProcedures

    hi guys,

    I also forgot and did not used the Set NoCount On option. Thanks brucevde and guys.
    --Kishore...

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