How to read the last row with SQL Server

后端 未结 17 823
别那么骄傲
别那么骄傲 2020-11-29 03:40

What is the most efficient way to read the last row with SQL Server?

The table is indexed on a unique key -- the \"bottom\" key values represent the last row.

17条回答
  •  借酒劲吻你
    2020-11-29 04:27

    I tried using last in sql query in SQl server 2008 but it gives this err: " 'last' is not a recognized built-in function name."

    So I ended up using :

    select max(WorkflowStateStatusId) from WorkflowStateStatus 
    

    to get the Id of the last row. One could also use

    Declare @i int
    set @i=1
    select WorkflowStateStatusId from Workflow.WorkflowStateStatus
     where WorkflowStateStatusId not in (select top (
       (select count(*) from Workflow.WorkflowStateStatus) - @i ) WorkflowStateStatusId from .WorkflowStateStatus)
    

提交回复
热议问题