You could also consider
SET ROWCOUNT @x;
SELECT Foo
FROM Bar
ORDER BY Baz;
Instead of
SELECT TOP (@x) Foo
FROM Bar
ORDER BY Baz;
The value you would need to set @x to is 0
to disable it.
This is deprecated for data modification statements but not deprecated for SELECT
.
In 2012 a different plan is compiled for the case that ROWCOUNT
is 0 vs some non zero value.
If the ORDER BY Baz
is only there to give meaning to the TOP
rather than to provide a presentation order for results and you don't have an index supporting this then splitting into two queries would avoid an unnecessary sort in the 0
case.
TOP ... ORDER BY
something? Is theORDER BY
something still required in the case that you select all?TOP
is out of the question? Like you're dealing with some predefined query and you have to pass it something?