Advertisement
Guest User

Untitled

a guest
Aug 28th, 2014
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.42 KB | None | 0 0
  1. ID ------ contact_id
  2.  
  3. 1 -------- 100
  4. 2 -------- 100
  5. 3 -------- 200
  6. 4 -------- 100
  7.  
  8. select top(1) ID from table_name where ID> 1 order by ID
  9.  
  10. DECLARE @ContactId INT
  11. SET @ContactId = 100
  12.  
  13. ;WITH ContactsCTE AS
  14. (
  15. SELECT ID,
  16. Contact_ID,
  17. ROW_NUMBER()OVER(ORDER BY ID) AS rn
  18. FROM Contacts
  19. WHERE Contact_Id > @ContactId
  20. )
  21. SELECT ID,
  22. Contact_ID
  23. FROM ContactsCTE
  24. WHERE rn = 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement