In this post, I will show how to select random records using the TABLESAMPLE clause and use it in different ways. The TABLESAMPLE clause takes a parameter that can be a percent or a sample number representing rows. This clause samples a percentage of
pages randomly. You will get a different result set each time you run a query with the TABLESAMPLE clause. That means that even if you specify to return 50 rows, you will only get an approximate number of rows, that can be less or more than 50 due to the random sampling logic.
Select X% of Records
Returns different set of rows each time the query is executed
Select X% of Records with same random sampling multiple times
Specify REPEATABLE with seed. REPEATABLE clause returns same set of rows every time that it is executed
Specify Number of Records instead of %(Percentage)
Important Points about TABLESAMPLE:
1. If you have upgraded from SQL 2000 to SQL Server 2005, make sure that the compatibility level of the database is set to at least 90.
2. TABLESAMPLE cannot be used on derived tables, OPENXML, Inline Table-Valued functions, linked server tables or a View.
Read more on TABLESAMPLE over here http://msdn.microsoft.com/en-us/library/ms189108(SQL.90).aspx
Did you like this post?
|subscribe via rss||subscribe via e-mail|
|print this post||follow me on twitter|