且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从T-SQL中的表中选择前N行?

更新时间:2023-02-06 10:34:37

 从用户选择top(@count)* 

如果 @count 是常数,可以删除括号:

 从用户中选择top 42 * 

(后者适用于SQL Server 2000,而前者至少需要2005年)


Is there any way to select, for example, first 10 rows of a table in T-SQL (working MSSQL)?
I think I saw something in Oracle defined as rownum meta variable, used in a following way

select * from Users where rownum<=10

But what about MSSQL?

select top(@count) * from users

If @count is a constant, you can drop the parentheses:

select top 42 * from users

(the latter works on SQL Server 2000 too, while the former requires at least 2005)