Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 SQL2000/2005/2008

Author  Topic 

vijays
Starting Member

2 Posts

Posted - 2010-04-23 : 08:27:58
Hi, can anyone let me know how to fetch/get last inserted row in sql server. Table have NO Primary key, Identity, it has only one column 'Employee Name'.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-23 : 08:29:28
does it have any audit column? column like dateadded,datemodified etc?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Llewellyn
Starting Member

13 Posts

Posted - 2010-04-23 : 08:33:31
declare @Name varchar(128)

select @Name = [Employee Name]
from TableName
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-04-23 : 08:34:56
quote:
Originally posted by vijays

Hi, can anyone let me know how to fetch/get last inserted row in sql server. Table have NO Primary key, Identity, it has only one column 'Employee Name'.


It is not possible

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-04-23 : 08:34:57
Not reliable possible if there isn't a column that could give you this information.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-04-23 : 08:35:38
ONE SECOND?



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-23 : 08:36:13
quote:
Originally posted by Llewellyn

declare @Name varchar(128)

select @Name = [Employee Name]
from TableName


how will this give last inserted record? you cant guaranteed value returned will be last inserted record value. there's no concept of first and last in sql table

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-04-23 : 08:36:44
quote:
Originally posted by Llewellyn

declare @Name varchar(128)

select @Name = [Employee Name]
from TableName


The result is random not neccessarily the last inserted value

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-04-23 : 08:37:40
Too many

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Llewellyn
Starting Member

13 Posts

Posted - 2010-04-23 : 08:40:41
It will give the last physical record that is saved on the hard drive in the table in its present order\state. What ever that record is, it will give it.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-04-23 : 08:42:38
quote:
Originally posted by Llewellyn

It will give the last physical record that is saved on the hard drive in the table in its present order\state. What ever that record is, it will give it.



Without a clustered index how is that possible?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Llewellyn
Starting Member

13 Posts

Posted - 2010-04-23 : 08:47:17
To test what I have said, try adding a clustered index to your table on the employee name. This will change the physical order of entries in the table and theatrically the highest alphabetical name should be the last in the table. Remove the clustered index and do the same request. The result should be the same. Your may want to backup your database before you mess with the order of the rows.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-23 : 08:50:53
quote:
Originally posted by Llewellyn

To test what I have said, try adding a clustered index to your table on the employee name. This will change the physical order of entries in the table and theatrically the highest alphabetical name should be the last in the table. Remove the clustered index and do the same request. The result should be the same. Your may want to backup your database before you mess with the order of the rows.


Nope not always you can guarantee you'll get last inserted value without clustered index

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-04-23 : 08:53:17
quote:
Originally posted by Llewellyn

To test what I have said, try adding a clustered index to your table on the employee name. This will change the physical order of entries in the table and theatrically the highest alphabetical name should be the last in the table. Remove the clustered index and do the same request. The result should be the same. Your may want to backup your database before you mess with the order of the rows.


What if there are already duplicate names in the table?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Llewellyn
Starting Member

13 Posts

Posted - 2010-04-23 : 08:55:30
How about a full factor of 100%. Would that not make a difference.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-26 : 23:36:01
quote:
Originally posted by Llewellyn

How about a full factor of 100%. Would that not make a difference.




Fill factor is not relevant to this discussion.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -