读书人

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

发布时间: 2011-12-28 22:45:21 作者: rapoo

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

读书人网 >SQL Server

热点推荐