clear markdown compare help best fiddles feedback dbanow.uk
clear markdown donate comments/suggestions/bugs a leap of faith? bible365 diddy dollings
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 2805438 fiddles created (40817 in the last week).

CREATE TABLE t1 (v1 int PRIMARY KEY, v2 int); CREATE TABLE t2 (v1 int, v2 int); INSERT INTO t1 VALUES (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9);
9 rows affected
 hidden batch(es)


DECLARE @top int = 4, @affectedRows int, @pointer1 int, @pointer2 int; INSERT INTO t2 SELECT TOP (@top) v1, v2 FROM t1 --WHERE <CONDITION> ORDER BY v1, v2; SET @affectedRows = @@ROWCOUNT; SELECT TOP 1 @pointer1 = v1, @pointer2 = v2 FROM (SELECT TOP (@top) v1, v2 FROM t1 --WHERE <CONDITION> ORDER BY v1, v2) T ORDER BY v1 DESC, v2 DESC; SELECT @affectedRows Rows, @pointer1 Pointer1, @pointer2 Pointer2;
Rows Pointer1 Pointer2
4 4 4
 hidden batch(es)