CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: Sql Query Help!

  1. #1
    Join Date
    Dec 2004
    Posts
    423

    Sql Query Help!

    I am using VB6 with SQL SErver 2000.

    I want to create a query the the last x records inputed.
    Anybody got any ideas?

  2. #2

    Re: Sql Query Help!

    If you're using an integer primary key, try something like:

    Code:
    select top 10 * from mytable order by id DESC
    Change for your requirements...

  3. #3
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Sql Query Help!

    If you want the last 10 records (meaning the latest inserted data into the table) then to achieve this you need to have a Timestamp field in your table. And then use the field Timestamp instead of 'id' as in mmetzger's post (order by clause). Regards.

  4. #4

    Re: Sql Query Help!

    Would an auto-increment id field perform the same thing? The values would never be reused so the highest values would be the last entries made.

  5. #5
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Sql Query Help!

    Quote Originally Posted by mmetzger
    Would an auto-increment id field perform the same thing? The values would never be reused so the highest values would be the last entries made.
    Yes, it would! But they work upon the restriction that a table must have an auto-increment id field which might not always be the case. Anyways, I have been wondering about this - what happens when we reach the upper limit for the value of an auto-increment id field due to frequent inserts and deletes? What value would the next record get? Regards.

  6. #6
    Join Date
    Dec 2004
    Posts
    423

    Re: Sql Query Help!

    Well Thank you for your replies.
    Is there any way I could do this without having a TimeStamp or AutoIncrement field?

  7. #7
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Sql Query Help!

    Alternative? why so? By the way, what you can do is make a small table and keep the information about the latest x records inserted over there - you might use the INSERT triggers for this purpose. Or, you could keep this information into some kind of a log file and then while retrieving use it. Regards.

  8. #8
    Join Date
    Dec 2004
    Posts
    423

    Re: Sql Query Help!

    Well I added a timestamp field but every adding a record I got an error. What I'll do is just create a regular smalldatetime field and insert GETDATE() for every record insterted.

    BTW: I read this about timestamp field
    http://msdn.microsoft.com/library/de...ta-tz_6fn4.asp
    Which is saying it really good as a unique value. Becuase it changes even if you modify a record.

    Thank you much

  9. #9
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up Re: Sql Query Help!

    Listen I did not say that the Identity column solution by mmetzger was not a solution but I said that it would depend if you had such a column in the table of interest.

    Now coming to datetime/timestamp. What you looked at is good you came to know about a new datatype. What I meant is make a column named DateStamp or TimeStamp and enter the values into that column yourself filling in the value i.e. making it a part of your insert statement. Now, if you want it not to change when you update the records then simply don't pass a value for this columns update. And now when you will select records based on timestamp/datestamp you will get the newest "inserted" records. Do not let the database fill in the value for you. So your insert statement would look something like:
    INSERT INTO <tablename> values (<field1>, <field2>,...,<timestamp for example: '10/11/2005 12:00:00'>) //as per the database's format of acceptance...
    Where from will you get this datetime value? Using a function that would give you the time and date for "now". Remember not to update this value in the update statement if your main aim is to achieve selecting newest "inserted" records. Hope this is clear. Regards.
    Last edited by exterminator; November 10th, 2005 at 03:04 PM.

  10. #10
    Join Date
    Oct 2005
    Location
    India
    Posts
    24

    Lightbulb Re: Sql Query Help!

    Dear All,

    I want to now one thing.what he ased is to get the last ten inserted records.I want to know whether that includes the last updated records also.Because the last modified record also can be included as the last inserted records.

    so in this case the timestamp will be of really help.If there is any suggesstion about this please tell.



    Thank you.

  11. #11

    Re: Sql Query Help!

    You'd need to have a datetime field that had an update time added when you update the row. You'd then modify the query to sort by that field.

  12. #12
    Join Date
    Jul 2004
    Location
    Jakarta, Indonesia
    Posts
    596

    Re: Sql Query Help!

    I have been wondering about this - what happens when we reach the upper limit for the value of an auto-increment id field due to frequent inserts and deletes?
    i've been wondering about this one too but i guess no one lives long enough to tell the truth
    Where from will you get this datetime value?
    i guess what Sabin_33 means is using TimeStamp Data Type not Columns that contain date and time value like exterminator, mmetzger means

    1st NF - a table should not contain repeating groups.
    2nd NF - any fields that do not depend fully on the primary key should be moved to another table.
    3rd NF - there should be no dependency between non key fields in same table.
    - E. Petroutsos -


    eRiCk

    A collection of "Laku-abis" Ebook, Permanent Residence

    Access Reserved Words, a Classic Form Bug, Access Limitation, Know run Process and the Lock they hold in, Logging User Activity in MSSQL

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured