Thursday, September 12, 2013

Select TOP 2 results in variables without loop

Select TOP 2 results in variables without loop

I want to store the top 2 results in 2 variables.
create table t(id int);
insert into t (id) values (1),(2),(3),(4);
declare @id1 int
declare @id2 int
select top 2 @id1 = first id, @id2 = next id
from t
SQLFiddle
Can I do it in one query without using a loop?

No comments:

Post a Comment