如何在SQL Server 2008中创建存储过程来获取前一列Entry的值

本文关键字:的值 Entry 一列 获取 SQL Server 2008 存储过程 创建 | 更新日期: 2023-09-27 18:01:53

数据库表:UserTime

UserTimeId int(pk)
LoginStatus nvarchar

LoginStatus只存储两条信息:InOut

,

UserTimeId    LoginStatus
--------------------------
    1           In
    2           Out
    3           In
    4           Out   

我想创建一个存储过程来检查前一列的LoginStatus的值。

在c#代码中,我想这样实现它:
if (StoredProcedureCheckLoginStatus='In')
{
   dothis...
}
else 
{
   do this....
}

如何在SQL Server 2008中创建存储过程来获取前一列Entry的值

使用CTE。示例连接:

WITH CTE as (
   SELECT 
     RN = ROW_NUMBER() OVER (ORDER BY EmployeeID),
     * 
   FROM HumanResources.Employee
 )
 SELECT
   [Previous Row].*,
   [Current Row].*,
   [Next Row].*
 FROM CTE [Current Row]
 LEFT JOIN CTE [Previous Row] ON 
   [Previous Row].RN = [Current Row].RN - 1
 LEFT JOIN CTE [Next Row] ON 
   [Next Row].RN = [Current Row].RN + 1
 WHERE
       [Current Row].EmployeeID = 5