怎么读取表的最后几条记录?
有一个表,表的主码是个标识列,但是主码值不连续。现在要一次读取20条记录。第一取最后的20-40条记录,第三次读取最后的40-60条记录。用存储方法的代码怎么次读取表的最后0-20条记录,第二次读写?
------解决方案--------------------declare @sql varchar(1000)
declare @次数 int
declare @总长 int
select @总长=count(*) from #table
set @sql= ' '
select @sql=@sql+ 'select top 20 * from (select top '+ltrim(@总长-(@次数-1)*20) + ' * from #table order by id) t order by t.id desc '
print @sql
------解决方案----------------------如何从表中用SELECT语句提取最后10(N)条数据呢
declare @num as int
select @num = count(*) from authors
set @num = @num - 10 --N
declare @sql as varchar(200)
set @sql = 'select * from authors where au_id not in (select top ' + cast(@num as char) + ' au_id from authors) '
exec (@sql)