Results 1 to 16 of 16

Thread: [2008] SQL CMD get last row in a column

  1. #1

    Thread Starter
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    [2008] SQL CMD get last row in a column

    I want to get the last row in my colum 'ID'

    such as:
    Dim sqlCommand As New SqlCommand("SELECT LASTROW of ID FROM Table4")

    however, LASTROW, or LAST is not exactly a valid command, basically I want to count how many entries have been added, since ID has AutoIncrement, then the value of the last row, will be the correct amount.

    Can anyone tell me how to do this?

    Thanks

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [2008] SQL CMD get last row in a column

    How about "SELECT MAX(ID) FROM blah blah"

  3. #3
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    530

    Re: [2008] SQL CMD get last row in a column

    OR
    vb Code:
    1. SELECT Top 1 *  FROM YourTable order by ID desc

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] SQL CMD get last row in a column

    That's actually the not the best approach. In some cases it will give the correct result but in others it will not. If you delete a row or roll back a transaction at any point then it will give you an incorrect result. Even if you aren't deleting any rows or using any transactions you should still do it the "correct" way from the start. If you want to know how many rows there are in a table then you should count them:
    Code:
    SELECT COUNT(*) FROM MyTable
    Last edited by jmcilhinney; Feb 3rd, 2008 at 06:24 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: [2008] SQL CMD get last row in a column

    Perfect, thanks

    Edit:
    Thanks for the correct jmcilhinney.
    Last edited by Icyculyr; Feb 3rd, 2008 at 06:43 AM.

  6. #6
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [2008] SQL CMD get last row in a column

    If the ID is a indentity column then the count(*) is definitely not the correct way to go. The only truly valid reason to use count is to see how many rows are there or the number of rows that meet a specified condition. If you want the ID of the last inserted row and the ID column is an Identity you should issued the Select Scope_Identity() as a second command in the insert statement
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [2008] SQL CMD get last row in a column

    So many different answers to such a vague question.

    Gary might have picked up on the real reason it's being asked - but again - the OP should be a lot more specific so that the proper response can be given.

    What do you need this "last" value for? When are you trying to get it - right after you do an INSERT? Is this a multi-user application?

    And of course - what is your backend DATABASE??

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8

    Thread Starter
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: [2008] SQL CMD get last row in a column

    when I retrieve the values, I need to know how many there are, so I don't go past the amount there are..

    For example, a list of strings, (40) to be precise, you click the "R" button, and it shows you the next string, so if you click R 41 times, then problems occur, I want to be able to count the rows, to see how far I am going, or get the last value in my ID column, whichever is most suitable.

    Thanks

  9. #9
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [2008] SQL CMD get last row in a column

    Well - with that clarification then I don't believe that any of the solutions fit properly.

    What is the code you are using to move "R" - and where do your records reside - what kind of UI control are you using?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  10. #10

    Thread Starter
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: [2008] SQL CMD get last row in a column

    R is a button, and you click it.

    Records are in a database, in Table4, in Column textValueCol,

    what kind of UI? a label, that shows the string lol.

    Cheers

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] SQL CMD get last row in a column

    It seems to me that this doesn't warrant a query at all. Get all your data into a DataTable and bind it to your Label via a BindingSource. You then call the MoveNext method of the BindingSource to advance to the next record until Position is equal to (Count-1).
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: [2008] SQL CMD get last row in a column

    I see, so a Table in a database can't do that, but the data from a datatable can, What is a binding source? and what does that do?

    Thanks

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] SQL CMD get last row in a column

    What did MSDN tell you when you looked there?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14

    Thread Starter
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: [2008] SQL CMD get last row in a column

    It told me ask jmcilhinney because he knows more.

    I'll go check MSDN now.

  15. #15
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [2008] SQL CMD get last row in a column

    Quote Originally Posted by Icyculyr
    I see, so a Table in a database can't do that, but the data from a datatable can, What is a binding source? and what does that do?

    Thanks
    You visit the database to get data. You limit the visits.

    It's always best to grab data from the DB (that's the server) and bring it to the client for processing.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  16. #16

    Thread Starter
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: [2008] SQL CMD get last row in a column

    Ok, so you store the data in the DataSet?, and access the DataSet correct?

    Cheers

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