Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
819 views
in Technique[技术] by (71.8m points)

sql - TOP versus SET ROWCOUNT

Is there a difference in performance between TOP and SET ROWCOUNT or do they just get executed in the same manner?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes, functionally they are the same thing. As far as I know there are no significant performance differences between the two.

Just one thing to note is that once you have set rowcount this will persist for the life of the connection so make sure you reset it to 0 once you are done with it.


EDIT (post Martin's comment)

The scope of SET ROWCOUNT is for the current procedure only. This includes procedures called by the current procedure. It also includes dynamic SQL executed via EXEC or SP_EXECUTESQL since they are considered "child" scopes.

Notice that SET ROWCOUNT is in a BEGIN/END scope, but it extends beyond that.

create proc test1
as
begin
    begin
    set rowcount 100
    end
    exec ('select top 101 * from master..spt_values')
end
GO

exec test1
select top 102 * from master..spt_values

Result = 100 rows, then 102 rows


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...