declare MyCursor Cursor
global scroll static
for select select distinct 班级号 from table
open MyCursor
declare @deptID smallint
fetch next from MyCursor into @deptID
while (@@fetch_status=0)
begin
insert into @t (studentno,classno) select top 3 studentno,classno from table where classno =@deptID
fetch next from MyCursor into @deptID
end
close MyCursor
deallocate MyCursor
select * from @t