SQL Server - 使用参数选择结果集的前 X [英] SQL Server - use a parameter to select the top X of the result set

查看:30
本文介绍了SQL Server - 使用参数选择结果集的前 X的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 SQL Server 查询,该查询将采用一个参数并将其用作要返回的记录号.

I am creating a SQL Server query that will take a parameter and use that as the record number to return.

在伪代码中:

parameter returnCount

select top returnCount * from table where x = y

执行该操作的正确语法/代码是什么?

What is the correct syntax/code to perform that operation?

推荐答案

在 SqlServer 2005 及更高版本中,执行以下操作:

In SqlServer 2005 and up, do this:

CREATE PROCEDURE GetResults    (
    @ResultCount   int
)
AS

SELECT top(@ResultCount) FROM table where x = y

对于早期版本,请使用:

For earlier versions, use:

CREATE PROCEDURE GetResults    (
    @ResultCount   int
)
AS

SET ROWCOUNT @ResultCount

SELECT * FROM table where x = y

http://www.4guysfromrolla.com/webtech/070605-1.shtml 了解更多信息.

这篇关于SQL Server - 使用参数选择结果集的前 X的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆