简体   繁体   中英

How to create stored procedure to get value of previous column Entry in SQL Server 2008?

Database table : UserTime

UserTimeId int(pk)
LoginStatus nvarchar

LoginStatus stores only two pieces of information: In and Out

Ex:

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

I want to create a stored procedure to check what is the value of LoginStatus of the previous column.

In C# code I want to implement it as this:

if (StoredProcedureCheckLoginStatus='In')
{
   dothis...
}
else 
{
   do this....
}

Use a CTE. Sample attached:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM