Inserting data on top-help..

Results 1 to 2 of 2

Thread: Inserting data on top-help..

  1. #1
    Join Date
    Dec 1969

    Default Inserting data on top-help..

    Hi all,<BR><BR>I am trying to insert data from a form which has 4 fields such that the data always sits at the top in my database occupying locations :<BR><BR>SEC_ID: 1, 2 , 3 & 4 <BR><BR>Everytime i insert data the data gets appended at the end i,e, if the first 4 fields are occupied my data takes the postions 5,6,7 & 8 but this wont do..<BR><BR>Can anyone pl help me out with a suitable query to achieve the same??<BR><BR>TIA and hv a great weekend,<BR>sandeep

  2. #2
    Join Date
    Dec 1969

    Default No such thing as top in a DB

    ...or bottom, for that matter.<BR><BR>When you add data to a table, it simply goes in the next available "slot". So if you never delete data, then for most DBs the added data will go at the end of the table. But if, for example, you deleted record #3 and then added record #99, you very well could find that the order of the records in the DB is 1-2-99-4-5-...<BR><BR>If you want ordering in a DB, you have to add it yourself. Give each record a number, as you add it, and then you can control which records you pull in the next query. The most common way to do this is with an *automatic* field of some kind that increments by one as each record is added. "Counter" or "AutoIncrement" in Access. "Identity" in SQL Server. Etc.<BR><BR>For your example, if you numbered your records then you could do this:<BR><BR>SELECT TOP 4 * FROM table ORDER BY recordNumber DESC<BR><BR>The "DESC"ending asks to reverse the order of the record numbers; the TOP 4 says give me only 4 records in the order specified. So you&#039;d get the last 4 records added. They would be in reverse order, but you could easily then re-reverse the order as you generate your HTML display.<BR><BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts