Friday, 1 July 2011

Select TOP @recordCount in TSQL

This is how you can limit the results from a table based on a parameter's value in non dynamic TSQL

declare @recordCountint = 100

SELECT TOP (@recordCount)

*

FROM MyTableName

All you have to do is not forget the enclosing brackets around the variable in the SELECT TOP clause and it works! Without these brackets you see the following error...

Msg 102, Level 15, State 1, Line 3
Incorrect syntax near '@recordCount'.

No comments:

Post a Comment

Android mediaserver process killing battery life

If this happens to you, then do the following Force stop on Settings, Applications, All, Media Storage. Clear Cache and Clear Data, fo...