SQL - How to find the highest number in a column?

前端 未结 15 1078
借酒劲吻你
借酒劲吻你 2020-12-30 18:25

Let\'s say I have the following data in the Customers table: (nothing more)

ID   FirstName   LastName
-------------------------------
20   John        Macken         


        
15条回答
  •  情书的邮戳
    2020-12-30 19:25

    If you've just inserted a record into the Customers table and you need the value of the recently populated ID field, you can use the SCOPE_IDENTITY function. This is only useful when the INSERT has occurred within the same scope as the call to SCOPE_IDENTITY.

    INSERT INTO Customers(ID, FirstName, LastName)
    Values
    (23, 'Bob', 'Smith')
    
    SET @mostRecentId = SCOPE_IDENTITY()
    

    This may or may not be useful for you, but it's a good technique to be aware of. It will also work with auto-generated columns.

提交回复
热议问题