| 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 MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Llewellyn
Starting Member
13 Posts |
Posted - 2010-04-23 : 08:33:31
|
| declare @Name varchar(128)select @Name = [Employee Name]from TableName |
 |
|
|
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 possibleMadhivananFailing to plan is Planning to fail |
 |
|
|
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. |
 |
|
|
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. |
 |
|
|
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 MVPhttp://visakhm.blogspot.com/ |
 |
|
|
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 valueMadhivananFailing to plan is Planning to fail |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-04-23 : 08:37:40
|
Too many MadhivananFailing to plan is Planning to fail |
 |
|
|
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. |
 |
|
|
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?MadhivananFailing to plan is Planning to fail |
 |
|
|
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. |
 |
|
|
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 MVPhttp://visakhm.blogspot.com/ |
 |
|
|
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?MadhivananFailing to plan is Planning to fail |
 |
|
|
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. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
|