惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Know Your Adversary
Know Your Adversary
Latest news
Latest news
H
Help Net Security
C
CERT Recently Published Vulnerability Notes
阮一峰的网络日志
阮一峰的网络日志
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
Cyber Attacks, Cyber Crime and Cyber Security
Security Latest
Security Latest
B
Blog RSS Feed
V
Vulnerabilities – Threatpost
T
Threatpost
量子位
P
Proofpoint News Feed
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Cisco Talos Blog
Cisco Talos Blog
F
Fortinet All Blogs
Cyberwarzone
Cyberwarzone
Recorded Future
Recorded Future
博客园 - 聂微东
博客园 - 叶小钗
云风的 BLOG
云风的 BLOG
Forbes - Security
Forbes - Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
小众软件
小众软件
N
Netflix TechBlog - Medium
aimingoo的专栏
aimingoo的专栏
酷 壳 – CoolShell
酷 壳 – CoolShell
I
InfoQ
Last Week in AI
Last Week in AI
N
News and Events Feed by Topic
Google DeepMind News
Google DeepMind News
宝玉的分享
宝玉的分享
Hugging Face - Blog
Hugging Face - Blog
PCI Perspectives
PCI Perspectives
T
Tenable Blog
C
Cybersecurity and Infrastructure Security Agency CISA
L
LangChain Blog
SecWiki News
SecWiki News
H
Hacker News: Front Page
WordPress大学
WordPress大学
www.infosecurity-magazine.com
www.infosecurity-magazine.com
S
Schneier on Security
H
Heimdal Security Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
博客园 - 【当耐特】
A
About on SuperTechFans
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Tailwind CSS Blog

博客园 - 无之无

别了博客园! 即将没落的博客园?!!! 阿里云的背后故事(希望别被关了) LINQ:是BUG还是~~~ 集合代理类的实现 WPF对绑定到数据的树初始化选择项 SilverLight中的数据绑定 [请教]关于 .NET 应用的几个问题请教!!!( 如何解决 自定义控件 构造被设计器删除? 遭遇品牌电脑的尴尬 面对VS2005,我遇到了BUG? FOREACH的遍历 该死的微软,该死的文档,该死的ExecuteCommand Windows Form的初始化大小记录处理问题 怪异的Windows Form事件 MDI窗体关闭问题解决一例 .Net再发行包与强名称 托管与非托管的混合编程问题 关于WMI
SQL里ROWCOUNT的使用
无之无 · 2012-07-03 · via 博客园 - 无之无

一直不学无术,所以也就一直懵懵懂懂。

之前有朋友问起以下代码怎么不行:

declare @rowcount int;
set @rowcount = 10;
select top @rowcount * from table

这个是因为,TOP 只接受常量做数量参数,而不能接受变量,给对方提供的解决方案是:

declare @sql nvarchar(4000);
set @sql = 'select top ' + convert(varchar(10), @rowcount+ ' * from table'

exec(@sql)

刚在博问里,人问起 rowcount 的用法,查了下,文档里是这样说的:


使 SQL Server 在返回指定的行数之后停止处理查询。

主题链接图标 Transact-SQL 语法约定

语法


SET ROWCOUNT { number | @number_var } 

参数

number | @number_var

在停止特定查询之前要处理的行数(整数)。

注释

权限

要求具有 public 角色的成员身份。

示例

SET ROWCOUNT 在达到指定的行数后停止处理。请注意,在下面的示例中共有 545 行满足 Quantity 小于 300 的条件。但是,从更新后返回的行数可以看出并非所有行都得到了处理。ROWCOUNT 影响所有的 Transact-SQL 语句。


复制代码
USE AdventureWorks2008R2; GO SELECT count(*) AS Count FROM Production.ProductInventory WHERE Quantity < 300; GO

下面是结果集:

Count

-----------

537

(1 row(s) affected)

现在,将 ROWCOUNT 设置为 4,并以 Quantity 小于 300 的条件更新所有行。


复制代码
SET ROWCOUNT 4; UPDATE Production.ProductInventory SET Quantity = 400 WHERE Quantity < 300; GO

(4 row(s) affected)


于是醍醐灌顶,对朋友的问题得出新的解决方案:

declare @rowcount int;
set @rowcount = 1;
set rowcount @rowcount

SELECT * FROM table