Results 1 to 8 of 8

Thread: [Solved] Return a Value after an insert

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2004
    Posts
    18

    Resolved [Solved] Return a Value after an insert

    Thanks for looking,

    I need to get a new Id from a table that just added a new record. How can I go about getting the returned value back, after this insert is done I need to use that ID for other statments.
    Last edited by DougieC; Nov 4th, 2004 at 04:17 PM.

  2. #2
    Fanatic Member vb_dba's Avatar
    Join Date
    Jun 2001
    Location
    Somewhere aloft between the real world and insanity
    Posts
    1,016
    Are you using SQL to insert the new record? If so, are you using SQL Server, Access, Oracle, etc as your database engine?

    If you are using the Recordset.AddNew/Recordset.Update method to add a new record, and the ID field is an autokey (Access) or Identity(SQL Server) column, then you can reference the column name after the .Update statement is issued for the new ID.

    VB Code:
    1. With oRS
    2.     .AddNew
    3.  
    4.     'Assume you table has 3 columns: usr_id (autokey), fname (text), lname(text)
    5.     .Field("fname").Value = "blah"
    6.     .Field("lname").Value = "blah-blah"
    7.  
    8.     .Update
    9.  
    10.     Msgbox "ID is " & .Field("usr_id").Value
    Chris

    Master Of My Domain
    Got A Question? Look Here First

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2004
    Posts
    18
    Sorry, I am using vb.net and sql server and I have no idea what Recordset.AddNew/Recordset.Update method is . I am doing the insert as such
    Code:
    query = "INSERT INTO NOTE(Modified_Date, Regarding,
     Entered_By_ID)" & _
    " VALUES(@Modified_Date, @Regarding, 
    @Entered_By_ID)"
    if that helps any

  4. #4
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    Then execute the following after the Insert. Return its value into a datareader or try the ExecuteScalar method.

    Select @@Identity or Select @@Scope_Identity

    or use stored procedures and Output parameters.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2004
    Posts
    18
    Could you show me how to use the Select @@Identity or Select @@Scope_Identity in vb.net I seem to be getting an erorr

  6. #6
    Fanatic Member vb_dba's Avatar
    Join Date
    Jun 2001
    Location
    Somewhere aloft between the real world and insanity
    Posts
    1,016
    Not sure about vb.Net, but in SQL Server, you can create a stored procedure with an Output parameter that you can utilize to return the id:
    Code:
    Create Procedure db_abc @Name varchar(10)
                        , @ReturnParm int Output
    
    As
    
    --If the id field is an identity
    Insert Into myTable(emp_name)
    Values (@Name)
    
    Select @ReturnParm = @@Identity
    
    --if id field is a non-identity numeric field
    /*
    Select @ReturnParm = Max(id_fld) + 1 From myTable
    If @ReturnParm Is NULL Set @ReturnParm = 1
    
    Insert Into myTable (id_fld, emp_name)
    Values (@ReturnParm, @Name)
    */
    In VB6, I'd create an ADO Command object and create an output parameter. Then I'd reference this output parameter after I executed the stored procedure:
    VB Code:
    1. Dim oCmd  As ADODB.Command
    2.  
    3. Set oCmd = New ADODB.Command
    4. With oCmd
    5.      .ActiveConnection = oCN    'oCN is my connection Object
    6.     .CommandType = adCmdStoredProc
    7.     .CommandText = "db_abc"
    8.     .Parameters.Append .CreateParameter("@Name", adVarChar, adParamInput, 10, sName)
    9.     .Parameters.Append .CreateParameter("@ReturnParam", adInteger, [b]adParamOutput[/b], 4)
    10.  
    11.     .Execute
    12.     MsgBox .Parameters("@ReturnParam").Value
    13. End With
    Not sure how you would translate the VB6 part to .Net.
    Chris

    Master Of My Domain
    Got A Question? Look Here First

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Oct 2004
    Posts
    18
    Thanks vb_dba and brucevde that totally helps me out, Thanks for the help

  8. #8
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492
    Originally posted by DougieC
    Thanks vb_dba and brucevde that totally helps me out, Thanks for the help
    You should not rely on @@IDENTITY since it can return bogus data. Use the other suggestion and take @@SCOPE_IDENTITY

    Jon

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