日期:2014-05-19  浏览次数:20421 次

如何检索整个表,但不要最后一行?
SQL可以通过语句
select   top   n   *   from   tab   where   somefield=somevalue  
检索到前n行的数据

但是现在是,想检索整个表,但是最后一行不要,怎么办?

------解决方案--------------------
declare @n int
select @n=isnull(count(*)-1, 0) from tab where somefield=somevalue
exec( 'select top '+@n+ ' * from tab where somefield=somevalue ')

------解决方案--------------------
if(@i> 0)
没有then
------解决方案--------------------
最后一个是最新的吗?

select * from tab where id <> (select top 1 t1.id from tab t1 order by t1.datetime desc)
------解决方案--------------------
set nocount on
declare @n int
select @n=count(*) from tab where somefield=somevalue
if @n> 1
set @n=@n-1

set rowcount @n
select * from tab where somefield=somevalue
set rowcount 0
SET NOCOUNT OFF