I want to have a variable for selecting top rows. I can select top rows based on a variable. However I want to select all rows if the variable is not supplied.
Currently I’m using this query:
DECLARE @TOPCOUNT int;
SET @TOPCOUNT=10;
SELECT TOP(@TOPCOUNT) * FROM TABLE1
Update:
-
The original query is very lengthy and complex, so I don’t to rewrite the entire query without top count in else clause.
-
I don’t want to use dynamic query because of its repercussions.
Something like this:
Added after above UPDATE – if this is a parameter of a Stored Procedure then just provide a default for
@TOPCOUNT: