首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

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

2011-12-28 
如何检索整个表,但不要最后一行?SQL可以通过语句selecttopn*fromtabwheresomefieldsomevalue检索到前n行

如何检索整个表,但不要最后一行?
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

热点排行