select[top n]中关于查询纪录条数n的问题。可否为变量?
表里面有一个字段year_month 纪录数据写入时间
要求只保留12个月的,
思路:从里面取出year_month纪录的个数count
按照升序排列,取出前(count-12)个。
可是再写存储过城时始终不通过,是不是TOP后面不能带一个变量啊?
如果不能哪位更好的办法?
declare @nCountMonth int//总共记录个数
declare @nTempCount int//去掉12个月还剩下的个数
select @nCountMonth = count(distinct year_month) from month
if @nCountMonth > 12
begin
select @nTempCount = @nCountMonth -12
end
select distinct top @nTempCount year_month from MONTH //有问题??
order by year_month ASC