Solved

Sql Server 2008 Express R2 select max id

Posted on 2013-02-02
6
894 Views
Last Modified: 2013-02-17
I am using select max id in a query (SELECT MAX(id) FROM table), but it gives me the impression that sometimes the value returned is not the last id. What better way to get the last record.
0
Comment
Question by:rflorencio
6 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 150 total points
Comment Utility
Depending on how that id value is generated, the maximum value is not necessarily the last value added to the table.  (If you are using that as an identity column, then you would typically expect the max and the "last" value to be the same.)

Your query will always return the maximum value in that column at runtime.

Now, depending on how that id value is determined, and how frequently values are added to the column, that query result may get "out of date" very quickly...
0
 
LVL 7

Expert Comment

by:Robby Swartenbroekx
Comment Utility
If it is the key column of the table, it is better to configure it as an automaticaly incremental generated number.
0
 
LVL 42

Assisted Solution

by:EugeneZ
EugeneZ earned 150 total points
Comment Utility
SELECT MAX(id) FROM table is normally returns max  value
What made  you think it is incorrect?
\-----what is ID datatype in your table?


---

if id is identity int column


you can  use SCOPE_IDENTITY()
Returns the last identity value inserted into an identity column in the same scope. A scope is a module: a stored procedure, trigger, function, or batch. Therefore, two statements are in the same scope if they are in the same stored procedure, function, or batch.
0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 4

Expert Comment

by:rshq
Comment Utility
Hi

  Maybe type of Id is problem.
  For example if  type of Id is int or nvarchar sort order is different.
0
 
LVL 10

Assisted Solution

by:Monica P
Monica P earned 100 total points
Comment Utility
0
 
LVL 14

Assisted Solution

by:nishant joshi
nishant joshi earned 100 total points
Comment Utility
Rows in tables are sorted in order of key on which you have created clustered index.Clustered index puts data in sorted.

so max function will never give you last row.if your key is identity or always incremental then only max will give you last record.


Thanks,
Nishant
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now