DECLARE @top INT = 10;
SELECT TOP (@Top) *
FROM <table_name>;
4
votes
Its also possible to use dynamic SQL and execute it with the exec command:
declare @sqlnvarchar(200), @countintset @count = 10set @sql = N'select top ' + cast(@countasnvarchar(4)) + ' * from table'
exec (@sql)
4
votes
declare @rowsint = 10select top (@rows) *
from Employees
orderby1desc-- optional to get the last records using the first column of the table
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.OkRead more