944,183 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 1521
  • C# RSS
Nov 7th, 2009
0

How to put New record at the end of Database Table

Expand Post »
Dear Friends,

When i enter a new record in SQL Database from my C# application, the record stores in the database at random locations, but i want to make sure that every record must be enter at the end of the Table.






Actually i have to get the Last Balance of Customer, and it must be at the last record, but whenever i put a new record it stores at the middle or above, i need it to be store at the last so i get the last balance...


Plz help me out Dear Friends...
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
itslucky is offline Offline
56 posts
since Feb 2009
Nov 7th, 2009
0
Re: How to put New record at the end of Database Table
>How to put New record at the end of Database Table.
A table is an unordered recordset. The position of a record in the table is not relevant unless you impose an order on it.

If your table has transaction date column then use SELECT statement with ORDER BY clause.
text Syntax (Toggle Plain Text)
  1. str="select * from tableName order by transactiondate"
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Nov 7th, 2009
0
Re: How to put New record at the end of Database Table
The physical order of records written to an SQL table are determined by the clustered index if one is present. If one isn't present then they are just added in the order they are inserted and likewise they are returned in the order they are selected unless you use an order by clause.

You should add a "CreateDate" column or an auto incrementing record id column to determine which record is the last record. By default an autoinc primary key column creates the clustered index unless you specify to override it.

IE default value and clustered:
sql Syntax (Toggle Plain Text)
  1. CREATE TABLE Test
  2. (
  3. RecordId INT identity(1000, 1) PRIMARY KEY
  4. )

A nonclustered version:
sql Syntax (Toggle Plain Text)
  1. CREATE TABLE Test
  2. (
  3. RecordId INT identity(1000, 1) PRIMARY KEY NONCLUSTERED
  4. )

[edit]
Also if someone changes a clustered index that you are relying on to determine order than you cannot figure out by a column value then you run the risk of ruining your database by changing an index. This is a bad idea.... add another way to determine record sequence.
[/edit]
Last edited by sknake; Nov 7th, 2009 at 11:11 pm.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Nov 13th, 2009
0

Use Max(CreateDate)

Click to Expand / Collapse  Quote originally posted by sknake ...
...You should add a "CreateDate" column or an auto incrementing record id column to determine which record is the last record. By default an autoinc primary key column creates the clustered index unless you specify to override it.

[/edit]
After creating your table with the Create Date - or with the RecordID from Sknake above... then use Max(<fieldname>) to bring back the last entry.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jpattison is offline Offline
1 posts
since Nov 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: "ComException unhandeled by User Code"
Next Thread in C# Forum Timeline: Looking for a team of junior C# .NET programmers





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC