Hi
How I Can Get To Last Records in DataBase tthat User Enter?
darab
-4
Junior Poster in Training
Recommended Answers
Jump to PostIf the column is an identity the value can be retrieved by calling
SCOPE_IDENTITY()
Declare @ID int Insert Into aTable (aColumn) Values (aValue) Set @ID = Cast(SCOPE_IDENTITY() as int)
Jump to PostSELECT TOP 1 (columnWithYourID ) FROM tableName ORDER BY columnWithYourID DESC
That is bad advice. It creates a race condition with concurrency in SQL Server where it can return the wrong ID. Use
@@IDENTITY
orSCOPE_IDENTITY()
depending on what ID you need. Consult SQL Books Online for the difference in …
Jump to PostIf you want to get technical, you're advice is bad as well, as scope_identity will:
"SCOPE_IDENTITY(), like @@IDENTITY, will return the last identity value created in the current session, but it will also limit it to your current scope as well. In other words, it will return the last …
All 12 Replies
darab
-4
Junior Poster in Training
sknake
1,622
Senior Poster
Featured Poster
rapture
134
Posting Whiz in Training
sknake
commented:
no. That is unsafe as it creates a race condition.
-1
sknake
1,622
Senior Poster
Featured Poster
rapture
134
Posting Whiz in Training
sknake
1,622
Senior Poster
Featured Poster
rapture
134
Posting Whiz in Training
sknake
1,622
Senior Poster
Featured Poster
rapture
134
Posting Whiz in Training
sknake
1,622
Senior Poster
Featured Poster
rapture
134
Posting Whiz in Training
sknake
1,622
Senior Poster
Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.