|
-
Aug 12th, 2009, 03:02 PM
#1
Thread Starter
Lively Member
[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!!!
-
Aug 12th, 2009, 03:07 PM
#2
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 
-
Aug 12th, 2009, 03:11 PM
#3
Fanatic Member
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]
-
Aug 12th, 2009, 03:14 PM
#4
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 
-
Aug 12th, 2009, 03:15 PM
#5
Thread Starter
Lively Member
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!!!
-
Aug 12th, 2009, 03:16 PM
#6
Re: New SQL record
 Originally Posted by Seraph
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.
-
Aug 12th, 2009, 03:37 PM
#7
Thread Starter
Lively Member
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!!!
-
Aug 12th, 2009, 03:46 PM
#8
* 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 
-
Aug 12th, 2009, 03:53 PM
#9
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|