当前位置:Gxlcms > 数据库问题 > TSql Top 用法

TSql Top 用法

时间:2021-07-01 10:21:17 帮助过:5人阅读

table dbo.FinanceMonth (MonthNum int , quantity int ) ;with cte as ( select 1 as MonthNum,100 as quantity union all select MonthNum+1,quantity+100 from cte where MonthNum<12 ) insert into dbo.FinanceMonth select * from cte

2,使用 SET ROWCOUNT 限制返回的行数,但是MS不推荐使用这种方式,“在 SQL Server 的将来版本中,使用 SET ROWCOUNT 将不会影响 DELETE、INSERT 和 UPDATE 语句”。

SET ROWCOUNT 2

select *
from dbo.FinanceMonth

--等价于

select top 2 *
from dbo.FinanceMonth

取消限制,将 SET ROWCOUNT 指定为 0

SET ROWCOUNT 0

3,使用top限制DELETE、INSERT 和 UPDATE影响的行数,top后面的数字或百分数必须加上小括号,否则出错。

delete top (2)
from dbo.FinanceMonth

update top(2) dbo.FinanceMonth
set quantity=1

insert top(2) into  dbo.FinanceMonth
select * from dbo.FinanceMonth

 

TSql Top 用法

标签:

人气教程排行