SQL Server gets the previous data and the next data for the current ID

Source: Internet
Author: User

Conditions where the condition field is numeric select from tb  where [email protected];  --当前记录   select top 1 *  from tb  where id>@id  order by id;  --下一条记录   select top 1 *  from tb  where id<@id  order by id  desc --上一条记录Ii. a scheme based on a sort of thinking ; WITH TUsers  AS (      SELECT *, ROW_NUMBER() OVER ( ORDER BY vcUserID)  AS _ROW      FROM DB_Fast_Users.dbo.T_Users  AS A(NOLOCK) ) SELECT * into #T FROM TUsers  -- 上一条 select B.* from #T  AS A(NOLOCK) left join #T  AS B(NOLOCK)  ON B._ROW = (A._ROW - 1) where A.vcUserID= ‘Test1‘ -- 下一条 select B.* from #T  AS A(NOLOCK) left join #T  AS B(NOLOCK)  ON B._ROW = (A._ROW + 1) where A.vcUserID= ‘Test1‘ drop table #T
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.