Solved

Sql Server 2008 Express R2 select max id

Posted on 2013-02-02
6
907 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
ID: 38846836
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
ID: 38848145
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
ID: 38848808
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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 4

Expert Comment

by:rshq
ID: 38849365
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
ID: 38850020
0
 
LVL 14

Assisted Solution

by:nishant joshi
nishant joshi earned 100 total points
ID: 38850106
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

830 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