After a day of trying to figure out how to get a stored procedure to work for me, I ran across something that makes my life a heck of a lot easier. I always assumed this didn't work because I could never figure out the syntax, but luckily it does! Ever need to select a certain number of rows based off a variable? You can use the following to do just that:
SELECT TOP (@top) * FROM <table>
Now, if you need a certain number of random rows you can also use the following:
SELECT TOP (@top) * FROM <table> ORDER BY NEWID()
Note: This is only applicable in SQL Server 2005+. 2000 you need to use dynamic SQL or a subselect.
No comments:
Post a Comment