当前位置: 代码迷 >> Sql Server >> 怎么检索整个表,但不要最后一行
  详细解决方案

怎么检索整个表,但不要最后一行

热度:94   发布时间:2016-04-27 21:44:32.0
如何检索整个表,但不要最后一行?
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 '[email protected]+ ' * 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 @[email protected]

set rowcount @n
select * from tab where somefield=somevalue
set rowcount 0
SET NOCOUNT OFF
  相关解决方案