C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » SQL »

How to get the last record without any identity or Timestamp?


Posted Date: 07 Jan 2009    Resource Type: Code Snippets    Category: SQL
Author: Babu AkkandiMember Level: Diamond    
Rating: 1 out of 5Points: 10



Hi,

Introduction:-


I faced the Questions often like , How to get the last record inserted from the table?

Actual solution is, There's no way to determine the last inserted row without any identity or TimeStamp data.

But we can handle this problem in different way by writing procedure. have a look at this script

SQL Code :



In this stored procedure, we are using the cursor concept to fetch last record.

This Procedure accepts Tablename as input, and it returns last record from that table




create Procedure Get_LastRow
(
-- Table name to retrieve last record
@Tname Varchar(50)
)

AS
BEGIN
-- Cursor Declaration
-- By Default Cursor is Forward Only Cursor, by that we can't directly get the Last Record,
-- If we want to get the last record directly, we should go for dynamic cursor

EXECUTE ('DECLARE GETLAST CURSOR DYNAMIC FOR SELECT * FROM ' + @Tname)

OPEN GETLAST
FETCH LAST FROM GETLAST
CLOSE GETLAST

DEALLOCATE GETLAST
END



How To Use This:



To Execute this Procedure, follow the sample given below.

in this sample, we passed the tablename 'dbo.TableA' as parameter, it will return last record from the table if data exists, else return blank rows



EXEC Get_LastRow 'dbo.TableA'




Hope this Helps!



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
How to get the last record without any identity or Timestamp  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Split Date and Time using Sql in datetime Statement
Previous Resource: Find nth highest value
Return to Discussion Resource Index
Post New Resource
Category: SQL


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use