Results 1 to 9 of 9

Thread: [RESOLVED] New SQL record

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Location
    South Africa
    Posts
    88

    Resolved [RESOLVED] New SQL record

    Hi

    What is the best method to reference a new record added to SQL?
    High hopes go to all that looks down!!!

  2. #2
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: New SQL record

    can you provide more details?
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  3. #3
    Fanatic Member Seraph's Avatar
    Join Date
    Jul 2007
    Posts
    959

    Re: New SQL record

    A new record that has been added will be the last one in the table so:

    SELECT * FROM <table> ORDER BY id DESC LIMIT 1

    Should work. Or something similar.

    Visual Studio 2010 Professional | .NET Framework 4.0 | Windows 7

    SERYSOFT.COM :: SysPad - Folder Management Program - Please comment HERE if you find this program useful, have ideas, or know of any bugs.
    [Very useful for IT/DP departments where many folders are consistently accessed. Also contains a scratchpad window for quick access to notes.]

    [.NET and MySQL Quick Guide]

  4. #4
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: New SQL record

    more like

    Code:
    INSERT INTO TABLE (columnname) VALUES(value);SELECT SCOPE_IDENTITY
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Location
    South Africa
    Posts
    88

    Re: New SQL record

    Ok the way Im doing it now is to create an ID count table. So when I add a new record I set it's ID to the count table. I add one number to the count table once the record was created for the next record's ID.

    Let's use the following example:

    Table = Users
    Fiield1 = ID
    Field2 = Name

    Table = IDCount
    Field = ID

    So when I add a new User, I first reference the IDCounttable
    High hopes go to all that looks down!!!

  6. #6
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: New SQL record

    Quote Originally Posted by Seraph View Post
    A new record that has been added will be the last one in the table so:

    SELECT * FROM <table> ORDER BY id DESC LIMIT 1

    Should work. Or something similar.
    That will only work in a single user environment. if there are multiple users adding rows to the database, there is no way to guarantee that the newly added row is the row you just added.

    The question is, what do you need from that new row? If you are using some sort of auto incrementing ID, then your best bet is probably a stored proc that does the insert and then returns the ID to you, using a lock to ensure it gets the correct ID.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Location
    South Africa
    Posts
    88

    Re: New SQL record

    Hi Motil

    How will I use it in the following:

    Public Shared Sub Add_Camp(ByVal Number As Integer, ByVal Description As String)
    Dim myCon As New SqlClient.SqlConnection(strCon)
    Dim strSelect As String = "Insert into Camp(CampNr, Description) Values(@field1, @field2)"
    Dim myCommand As New SqlClient.SqlCommand(strSelect, myCon)

    With myCommand.Parameters
    .Add("@field1", SqlDbType.Int).Value = Number
    .Add("@field2", SqlDbType.VarChar).Value = Description
    End With

    myCon.Open()
    myCommand.ExecuteNonQuery()

    Dim iInt As Integer
    myCommand.CommandText = "SELECT SCOPE_IDENTITY() statement"
    iInt = myCommand.ExecuteScalar
    MsgBox(iInt)

    myCon.Close()
    myCon.Dispose()
    End Sub
    High hopes go to all that looks down!!!

  8. #8
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: New SQL record

    i just gave an example in this thread:

    http://www.vbforums.com/showthread.php?t=580073
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Location
    South Africa
    Posts
    88

    Re: New SQL record

    Motil, you are a real Angel.

    Thank you very much!!!!
    High hopes go to all that looks down!!!

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