读书人

关于select top 的有关问题

发布时间: 2012-02-15 12:09:44 作者: rapoo

关于select top 的问题,,,,求助!
select top 2* from tableA

如果tableA里只有一条纪录的话,这条语句,执行返回的是 1 条,而不是2条。

我该怎么处理,如果返回的不是2条,就打印错误。

总不能,

select count(*) from (select top 2* from tableA) as tableB吧?

谢谢!

[解决办法]
if(select count(*)from tb)> 1
begin
select top 2 * from tb
end
else
begin
select * from tb
union all select * from tb
end
[解决办法]
select top 2 * from tableA
if @@rowcount <> 2
print 'error '

读书人网 >SQL Server

热点推荐