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

How to find the last interactive logons in Windows using PowerShell

Use the following powershell script to find the last users to login to a box since a given date, in this case the 21st April 2022 at 12pm un...