I have set this little example of a strange behavior
SET NOCOUNT ON;
create table #tmp
(id int identity (1,1),
value int);
insert into
The behavior you see is rather subtle. By default, cursors in SQL Server are dynamic, so you would expect to see changes. However, buried in the documentation is this line:
SQL Server implicitly converts the cursor to another type if clauses in select_statement conflict with the functionality of the requested cursor type.
When you include the order by
, SQL Server reads all the data and turns it into a temporary table for sorting. In this process, SQL Server must also change the type of cursor from dynamic to static. This is not particularly well documented, but you can readily see the behavior.
I think that by putting in an ORDER BY clause it then forces the CURSOR to be a STATIC CURSOR whereas without it defaults to DYNAMIC.
You can use the sp_describe_cursor stored procedure to view the metadata of the cursor. Doing so on your example shows the following:
ORDER BY included:
model = Insensitive (or static), concurrency = Read-Only
ORDER BY excluded:
model = Dynamic, concurrency = Optimistic
Source: http://technet.microsoft.com/en-us/library/ms173806(v=sql.105).aspx